Java – how to create array objects when using parenthesis lists

I understand that when I call its constructor with the new keyword, an array object in Java will be created:

int[] myIntArray = new int[3];

But if I write instead

int[] myIntArray = {1,2,3};

An array object was created, but I didn't call its constructor with new How does this work – how do you create objects in Java without calling constructors?

Solution

In terms of creating array objects, it is a syntax sugar When compiled, it works exactly the same as the standard syntax

The difference is that for the first version, you didn't fill the array - all elements are the default value of int, which is zero

In the second version, you are creating and populating arrays

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>