Java的String数组差异 [英] Java String-array differences

查看:153
本文介绍了Java的String数组差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无聊,在做一些Java实践的检验,因为它已经很长一段时间,我用Java编程。有一定的问题后,我现在想知道下面的区别:

I'm currently bored and was doing some Java practice tests since it's been quite some time I've programmed in Java. After a certain question I'm now wondering the differences between the following:

String[] test1 = { "A", "B", "C" };
String[] test2 = new String[]{ "A", "B", "C" };
String test3[] = { "A", "B", "C" };
String test4[] = new String[]{ "A", "B", "C" };

当我在一个Java IDE输入这个我没有得到任何错误。我没有试图编译其中任何一个,但(虽然我主要使用测试2 自己的过去,所以我知道一件作品)。

When I type this in a Java IDE I'm not getting any errors. I haven't tried to compile any of them yet (although I've mainly used test2 myself in the past, so I know that one works).


  • 那么,是否每个这些编译?

  • 如果是的,有什么区别(如果有的话)?

  • (为测试1 性能比测试2 ,因为你不做出新的的String [] -instantiation,或者是这个反正做幕后?)

  • (如测试1 相同 TEST3 :为什么 TEST3 TEST4 在Java中存在,如果是一样的测试1 test2的?是否有任何情况下使用 [] 字段名的背后,是上述pferred类型背后$ p $?)

  • (是否有更多的(几乎相同)的变种除了这四个?)

  • (以及其他的问题,如这些。)

  • So, does each of these compile?
  • And if yes, what are the differences (if any)?
  • (Is test1 better for performance than test2 because you don't make a new String[]-instantiation, or is this done anyway behind the scenes?)
  • (If test1 is the same as test3: Why does test3 and test4 exist in Java if it's the same as test1 and test2? Are there any cases where using [] behind the field-name is preferred above behind the type?)
  • (Are there more (almost similar) variants apart from these four?)
  • (And other questions like these.)

只是想知道出于好奇。我们好好学习天天向上我想新的东西。

Just want to know out of curiosity. We learn something new every day I guess.

推荐答案

他们都是等价的。


      
  • 那么,是否每个这些编译?

  •   

是的,因为你自己也可以验证。

Yes, as you yourself can also verify.


      
  • 如果是的,有什么区别(如果有的话)?

  •   
  • (Test1的是性能比test2的更好,因为你不把一个新的String [] - 实例化,或者是这个反正做幕后?)

  •   

有编译字节code无差异。

There are no differences in compiled byte code.


      
  • (如果test1的是一样的TEST3:为什么TEST3和TEST4存在,如果是一样的TEST1和TEST2)

  •   

这只是写它作为数组索引可以在类型名称之后,以及标识符名称后放的另一种方式。唯一的区别是如果在一个行声明多个变量,如果索引的类型名称后放置,所有列出的标识符将是该类型的数组,而如果该索引标识符名称后放,只有标识符将是一个数组

It is just an alternative way to write it as the array indices can be put after the type name as well as after the identifier name. Only difference is if you declare multiple variable in one line, if indices are placed after the type name, all listed identifiers will be an array of that type, while if the indices are put after the identifier name, only that identifier will be an array.

示例:

String[] array1, array2;    // Both are arrays
String array3[], notArray4; // Only array3 is array

我推荐的类型后,将指数(的String []数组1 ),所以这将是unambigous。

I recommend putting indices after the type (String[] array1) so it will be unambigous.

(是否有更多的(几乎相同)的变种除了这四个?)

(Are there more (almost similar) variants apart from these four?)

不,我知道的。

这篇关于Java的String数组差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆