0

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.

Community
  • 1
  • 1
user1613360
  • 1,280
  • 3
  • 16
  • 42
  • Same thing as any other object -- `null`. You do realize that you can try this yourself, right? You're also probably prematurely optimizing if you're worrying about time time needed to initialize an array, if that requires any time at all – awksp Jun 07 '14 at 04:30
  • Yeah I can but I want to know how it will perfect the performance please read the question completely. – user1613360 Jun 07 '14 at 04:31
  • I did, and I answered your question, and you're almost certainly prematurely optimizing if you're worrying about how arrays are initialized, unless you're more interested in getting the reference from somewhere else. – awksp Jun 07 '14 at 04:33
  • @user3580294 You didn't answer my question you just took the question in your own way. – user1613360 Jun 07 '14 at 04:49
  • 1
    "I want to know what value will be assigned to the array data type in this auto generated constructor." I don't see how much clearer that gets. The value is `null`. – awksp Jun 07 '14 at 04:49

5 Answers5

3

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...

awksp
  • 11,764
  • 4
  • 37
  • 44
1

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.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • Here the accepted answer tells the array is filled with zeros.http://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java – user1613360 Jun 07 '14 at 04:33
  • @user1613360 Nope. See [here](http://ideone.com/BbKPyC). If you don't create the array explicitly you'll end up with `null`. – awksp Jun 07 '14 at 04:35
  • The statement int var[]=new int[10] will create an array with 10 zeros.you just initialized the array you didn't create it. – user1613360 Jun 07 '14 at 04:40
  • @user1613360 Except the default constructor doesn't do that. I'm using a default constructor, as you asked in your question. – awksp Jun 07 '14 at 04:40
  • @Anubian Noob The default constructor will initialize the array elements to zero.Check this http://ideone.com/A6ovCB – user1613360 Jun 07 '14 at 04:47
  • 1
    @user1613360 The constructor didn't do the initialization, as the initialization isn't **inside the constructor**. – awksp Jun 07 '14 at 04:49
  • The constructor will be auto generated by the JVM when you don't have a constructor. – user1613360 Jun 07 '14 at 04:50
  • @user1613360 And your initialization isn't part of the constructor. – awksp Jun 07 '14 at 04:50
  • I think you don't understand first I don't have a constructor in my class A and a constructor like A(){ initialize to default values } is auto generated.So I don't need to specify a constructor and please edit your answer. – user1613360 Jun 07 '14 at 04:52
  • @user1613360 You're wrong. The initialization is separate from the constructor. If you look at JLS 12.5, instance field initialization is a separate step from constructor execution. – awksp Jun 07 '14 at 04:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55241/discussion-between-user1613360-and-user3580294). – user1613360 Jun 07 '14 at 04:57
1

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.

Community
  • 1
  • 1
Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
0

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)

fasadat
  • 1,025
  • 1
  • 12
  • 27
0

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.

Nu2Overflow
  • 689
  • 1
  • 8
  • 16