Difference in array initialization

When viewing the source file, I saw two ways to initialize the array. I wonder if there is a difference between

int[] value = new int[0];

and

int[] value_next = new int[]{};

?

+3
source share
5 answers

There really is no difference. This is Syntactic sugarin the java array declaration.

The declaration of the first type is less confusing, at least for me :).

Note. I'm not sure why you specified the length as zero when declaring.

If possible, go to fooobar.com/questions/2086212 / ... for some advantages over others.

+6
source

Now the proof (and exercise):

, . , .class.
:

javap -c yourClass

-.

.

+2

.

int[] a = new int[0] , .

+1

, [].

int[] value_next = {} .

-, int[] value = new int[0]; , 0. 3 .

+1

, .

0 .

0

All Articles