user1613360
user1613360

Reputation: 1314

Java array initialization by class constructor(Performance issues)

Let's say I have classes A and B and the code is something like this:

class A
{
   private int x;
   private int[] y=new int[10];
   private string s;

}

public class B
{
  public static void main(String args[])

{
  A a=new A();
}

My understanding is when class A has no constructors defined JVM automatically defines a constructor like this:

//Auto generated constructor
A()
{
  x=0;
  s=NULL;
  y[]=//What will come here 
}

I want to know what value will be assigned to the array data type in this auto generated constructor.If it assigns zero to all the place holders doesn't it affect the performance of the program for large size arrays since assigning zero to all the members of the array is not a cheap task in terms of time.

Note:(UPDATE)

It doesn't store null.What is the default initialization of an array in Java? It store zeros.So my question is will it affect the performance for large arrays.

Upvotes: 0

Views: 306

Answers (5)

awksp
awksp

Reputation: 11867

From this test class:

public class Test {
    int[] x;

    public static void main(final String[] args) {
        System.out.println(new Test().x);
    }
}

You get this output:

null

If you use javac -XD-printflat to see what the compiler does with your source before compiling to bytecode, you get this:

public class Test {

    public Test() {
        super();
    }
    int[] x;

    public static void main(final String[] args) {
        System.out.println(new Test().x);
    }
}

And as x was never initialized directly, it will be null (can't find the relevant JLS passage at the moment, but perhaps I'll stumble upon it sometime.)


If you initialize x as new int[10], you end up with this processed source immediately before compilation to bytecode:

public class Test {

    public Test() {
        super();
    }
    int[] x = new int[10];

    public static void main(final String[] args) {
        System.out.println(new Test().x);
    }
}

So in any case, the field is initialized as a separate step from the rest of the constructor body. JLS 12.5 lays this out explicitly -- instance initializers and instance variable initializers are executed in a separate step from the rest of the constructor.


However, if you explicitly initialize the array using new int[size], you'll end up with an array of all 0s, as you noted. Whether this is a performance issue is really a moot question, as the JLS specifies explicitly what the default values for the array are, and you will not be able to escape the performance hit (if there is one). I'm looking at the JDK source right now to see if I can find out how arrays are created, but I suspect that large array creation overhead should be the least of your concerns...

Upvotes: 3

Aleksandr Podkutin
Aleksandr Podkutin

Reputation: 2580

I checked this with debugging.

It will be [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] because you set(initialize array) private int[] y = new int[10];.

If you don't initialize this array like this private int[] y, it will be null.

For your performance question, check this answers, please.

Upvotes: 1

Nu2Overflow
Nu2Overflow

Reputation: 699

In java all primitive types will be initialized to their default values. So if you define array of int type with their size then all the values will be initialized to 0. And primitive types have less overhead than the classes.

Upvotes: 0

fasadat
fasadat

Reputation: 1045

if you don't initialize the array like this private int[] y it return null but by using new like this private int[] y=new int[10]; it fills the array with default value of type array (in this case it is int)

Upvotes: 0

Anubian Noob
Anubian Noob

Reputation: 13596

It will be null. Arrays are treated like objects.

If you initialize an array (int[] a = new int[10]), it will fill the elements with their default values (such as zero for ints or null for objects and arrays).

However, the default constructor does not do that. It just sets the entire array to null.

Upvotes: 1

Related Questions