简体   繁体   中英

What is the difference between creating an array object and assigning arrays with curly braces?

In Java, I found some tutorials online and they teach differently when it comes to arrays,

Example 1:

Creating an array object using the "new" keyword then assigning values to it.

int[] values;
values = new int[5];

values [0] = 10;
values [1] = 20;
values [2] = 30;
values [3] = 40;
values [4] = 50;

System.out.println (values[2]); //Output : 30

Example 2:

Using curly braces to assign values to the array.

int[] values = {34,45,62,72}

System.out.println (values [2]); //Output : 62

What is the difference between the two examples?

The difference is only on the source code side.

That special syntax for array initialization using curly braces is nothing but syntactic sugar . Meaning: the compiler allows you to use a more concise way of expressing something. But there is nothing that you can do with that second style that you couldn't do with the other style.

Because, in the end, at runtime, the array gets created and its slots get assigned.

It is really only about making it easier for you to write source code that is easier to understand for human readers!

Well, one thing of course: the curly braces solution makes it impossible for you to provide "not enough" values. When you separate array creation and slot initialization it is possible to get the second step wrong somehow (for example by forgetting to init the last element of the previously created array).

And just to be sure: this is what you would see after compiling to .class and then disassembling using javap -c :

Example 1 code:

   0: iconst_5      
   1: newarray       int
   3: astore_1      
   4: aload_1       
   5: iconst_0      
   6: bipush        10
   7: iastore       

and Example 2:

   0: iconst_4      
   1: newarray       int
   3: dup           
   4: iconst_0      
   5: bipush        34
   7: iastore       

As you can see: the byte code instructions are absolutely identical (minus the different values you are pushing into the arrays)

  1. The first , declaration, initialisation, then assign values (detailed code)
  2. The second , declaration and assign values (less code same effect, you can call it two in one)

在示例1中,JVM为5个整数数组元素分配内存,每个元素用0初始化。如示例2所示,JVM再次为4个整数数组元素分配内存,但不是用0初始化它们,而是使用这些值直接初始化元素。用花括号指定。

From what I know both means the same. In first case your initialization can be anywhere on the same block whereas in second case you can only use at declaration and not other places.

The difference in these two lies in declaration of array and initialization of array and assignment of value to its element.

Method 1

int[] values; //array declaration
values = new int[5]; //array initialization, here memory will be allocated to values array.
values [0] = 10; //assignment of values
values [1] = 20;
values [2] = 30;
values [3] = 40;
values [4] = 50;

In Method 2 we are doing all these stuffs in single line of statement.

int[] values = {34,45,62,72}

If you talk about the functionality of these two, then they will both work same. it depends on you which way you want to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM