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.

asked Jun 7, 2014 at 4:29
5
  • 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 Commented Jun 7, 2014 at 4:30
  • Yeah I can but I want to know how it will perfect the performance please read the question completely. Commented Jun 7, 2014 at 4: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. Commented Jun 7, 2014 at 4:33
  • @user3580294 You didn't answer my question you just took the question in your own way. Commented Jun 7, 2014 at 4: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. Commented Jun 7, 2014 at 4:49

5 Answers 5

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

answered Jun 7, 2014 at 5:01
0
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.

answered Jun 7, 2014 at 4:31
11
  • Here the accepted answer tells the array is filled with zeros.stackoverflow.com/questions/3426843/… Commented Jun 7, 2014 at 4:33
  • @user1613360 Nope. See here. If you don't create the array explicitly you'll end up with null. Commented Jun 7, 2014 at 4: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. Commented Jun 7, 2014 at 4:40
  • @user1613360 Except the default constructor doesn't do that. I'm using a default constructor, as you asked in your question. Commented Jun 7, 2014 at 4:40
  • 1
    @user1613360 The constructor didn't do the initialization, as the initialization isn't inside the constructor. Commented Jun 7, 2014 at 4:49
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.

answered Jun 7, 2014 at 4:41
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)

answered Jun 7, 2014 at 4:41
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.

answered Jun 7, 2014 at 4:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.