Pierre Rymiortz
Pierre Rymiortz

Reputation: 1127

How are array object created when using a bracketed list

I understand an array object in Java is created when I call its constructor with the new keyword:

int[] myIntArray = new int[3];

But if I instead write

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

an array object gets created, but I haven't called its constructor with new. How does this work under the hood - how can an object be created in Java without calling the constructor?

Upvotes: 3

Views: 228

Answers (6)

Bohemian
Bohemian

Reputation: 425238

As far as creating the array object is concerned, it's syntactic sugar. When compiled, it works exactly like the standard syntax.

The difference here though is that with the first version, you aren't populating the array - all elements are the default value for int, which is zero.

With the second version, you're creating, and populating the array.

Upvotes: 7

Reimeus
Reimeus

Reputation: 159844

The first version populates an integer array with the default 0 value. The second assigns values explicitly.

The first version is equivalent to

int[] myIntArray = {0, 0, 0};

while the second is the same as

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

The new keyword is only mandatory for non declarative statements, for example .

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

Upvotes: 4

vijay
vijay

Reputation: 2694

Take this code and compile it:

public class ArrayTest {
    public static void main1() {
        int[] array = new int[3]; array[0] = 10; array[1] = 20; array[3] = 30;
    }

    public static void main2() {
        int[] array = new int[] {10, 20, 30};
    }

    public static void main3() {
        int[] array = {10, 20, 30};
    }
}

Then use javap -c to disassemble it to view its bytecode to get the following results. But what you will is that the later two snippets or methods compile to the same bytecode. So int[] array = new int[] {1, 2, 3} and int[] array = {1, 2, 3} are the same. But seperately creating an array and assigning values to each of its element is treated differently and so the later two snippets are not syntactic sugar for the first snippet.

$ javap -c ArrayTest
Compiled from "ArrayTest.java"
public class ArrayTest extends java.lang.Object{
public ArrayTest();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main1();
  Code:
   0:   iconst_3
   1:   newarray int
   3:   astore_0
   4:   aload_0
   5:   iconst_0
   6:   bipush  10
   8:   iastore
   9:   aload_0
   10:  iconst_1
   11:  bipush  20
   13:  iastore
   14:  aload_0
   15:  iconst_3
   16:  bipush  30
   18:  iastore
   19:  return

public static void main2();
  Code:
   0:   iconst_3
   1:   newarray int
   3:   dup
   4:   iconst_0
   5:   bipush  10
   7:   iastore
   8:   dup
   9:   iconst_1
   10:  bipush  20
   12:  iastore
   13:  dup
   14:  iconst_2
   15:  bipush  30
   17:  iastore
   18:  astore_0
   19:  return

public static void main3();
  Code:
   0:   iconst_3
   1:   newarray int
   3:   dup
   4:   iconst_0
   5:   bipush  10
   7:   iastore
   8:   dup
   9:   iconst_1
   10:  bipush  20
   12:  iastore
   13:  dup
   14:  iconst_2
   15:  bipush  30
   17:  iastore
   18:  astore_0
   19:  return

Upvotes: 2

Jojo John
Jojo John

Reputation: 400

Both statements are same. The second statement int[] myIntArray = {1,2,3}; is short cut to syntax using new method.

int[] myIntArray ={1,2,3} , this case length of the array is determined by the number of values provided between braces and separated by commas.

Upvotes: 2

jh314
jh314

Reputation: 27812

Under the hood, both ways of initializing an array is the same.

For another example, look at this:

String str = "Hello"; // no 'new' statement here

String str = new String("Hello");

Both statements do the same thing, but one is far more convenient than the other. But under the hood, they pretty much do the same thing after compilation.

Upvotes: 0

rgettman
rgettman

Reputation: 178303

This part:

{1,2,3}

is an array initializer that can be used as part of a declaration. To quote the JLS, section 10.6:

An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.

ArrayInitializer: { VariableInitializersopt ,opt }

Upvotes: 5

Related Questions