1

How come

int alone;
System.out.println(alone);

Gives errors but

 int[] arr = new int[1];
 System.out.println(arr[0]);

Equals 0? When you initialize an empty array, does it automatically initialize its contents to 0 (or null, etc.)?

onepiece
  • 3,279
  • 8
  • 44
  • 63

5 Answers5

6

From the language standard

Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).

And the default values are:

  • For type byte, the default value is zero, that is, the value of (byte)0.

  • For type short, the default value is zero, that is, the value of (short)0.

  • For type int, the default value is zero, that is, 0.

  • For type long, the default value is zero, that is, 0L.

  • For type float, the default value is positive zero, that is, 0.0f.

  • For type double, the default value is positive zero, that is, 0.0d.

  • For type char, the default value is the null character, that is, '\u0000'.

  • For type boolean, the default value is false.

  • For all reference types (§4.3), the default value is null.

Robert Cooper
  • 1,270
  • 9
  • 11
4

Yes, for primitive types(except boolean and char) it will be default to ZERO. If object type it will be default to null.

This java tutorial may help you.

NOTE: as woot4Moo answered, this is for only instance variables. If local variable, then it won't be default to any.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 2
    incorrect, boolean does not initialize to zero. Nor does char initialize to zero. – Woot4Moo Oct 31 '12 at 20:47
  • Instance array variables do not differ from local array variables since this initialization occurs when calling `new`. I don't think there is any way for `new` to differentiate between instance and local variables. – Code-Apprentice Oct 31 '12 at 20:50
  • 1
    @Code-Guru the distinction wasn't necessarily for the array but for the primitive type as was referenced by the first part of OP's question. – Woot4Moo Oct 31 '12 at 20:51
  • I thought instance variables are the ones within a class. From Woot4Moo's/Nambari's edit it seems that they do not include boolean and chars. – onepiece Oct 31 '12 at 20:52
  • 1
    @onepiece instance variables are those that are declared as part of the class that are **outside** of a function declaration. – Woot4Moo Oct 31 '12 at 20:52
  • Why won't local variable default to anything? I thought as long as you initialize an array (doesn't matter if local or instance), its contents will be set to default values. – onepiece Oct 31 '12 at 21:22
  • @onepiece: That is what whole discussion was about, in array case local/instance is not applicable (as you said, new will take care). But in primitive case, you need to explicitly assign some value to local variable before using them otherwise it is compile time error. – kosa Oct 31 '12 at 21:24
  • @onepiece When referring to local variables vs. class variables, we are not discussing the contents of an array. This is refers to the part of your question about why declaring a single `int` gives an error where as initializing an array does not. – Code-Apprentice Oct 31 '12 at 22:37
2

Yes, for primitive type number arrays, it initializes with 0, for boolean[], it initializes with false, for char[], it initializes with NULL(ASCII value 0) and for objects [] including String[] its initializes with null.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • @BheshGurung By the time I correct, I got two negative votes. Very fast :) – Yogendra Singh Oct 31 '12 at 20:47
  • and he is incorrect on `0` for numerics there are floats and doubles that get `0.0` – Woot4Moo Oct 31 '12 at 20:50
  • @BheshGurung: updated for several primitive type including boolean and char. – Yogendra Singh Oct 31 '12 at 20:58
  • @Woot4Moo: For floats or double, `0` is meant `0.0` as if you say float f = 0; and print f, it will print `0.0`. – Yogendra Singh Oct 31 '12 at 21:00
  • All is forgiven, I am about completeness when allowed. – Woot4Moo Oct 31 '12 at 21:00
  • @Woot4Moo: "I am about completeness when allowed" nothing can be called as complete as long as we don't just stick to the points in question. If we could have restricted ourself to int array and Object array, I don't think these many comments will be there. – kosa Oct 31 '12 at 21:07
2

There are two separate by similar issues involved here. First note that all variable types have a default value, which differs depending on the type. There are at least two times where these defaults are used: 1) declaring a member variable and 2) initializing an array with the new operator.

Notice that if you simply declare a local array variable without initializing it with new, then you get the same error as when you declare a simple int variable. This is because all local variables must be initialized. They do not get an automatic default value.

On the other hand, member variables do get a default value. Similarly, when you create an array object by using the new operator, the elements of the array are initialized to the appropriate default value.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

It depends on where it was declared (inside a class vice inside a function). If it is a class member variable it will be initialized to its default. 0 for numeric types(0.0 for float types / double), null for Strings, false for boolean,and null for Objects. If it is declared inside of a function it would remain uninitialized in the case of int alone. In terms of an array it will always initialize the values contained within it.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • Note that the OP is asking about the *elements* of an array after explicitly initializing the array itself with `new`. This is a different issue from member variables getting default values on declaration. – Code-Apprentice Oct 31 '12 at 20:53
  • I was still typing my comment =p – Code-Apprentice Oct 31 '12 at 20:53
  • @Code-Guru :) . Note the question has two parts. OP asked why `int alone`... gives errors but the array doesnt. It is of value to give the distinction for future readers and to the OP. Further the question does not explain where these two declarations exist, so it would have to be the case these are part of a function. – Woot4Moo Oct 31 '12 at 20:55
  • Perhaps you can clarify this in your answer. For example, When you say "where *it* was initialized", I now think you mean "where the variable was declared". I didn't get this interpretation the first time I read your answer. – Code-Apprentice Oct 31 '12 at 21:01
  • @Code-Guru I could see the confusion. I will update to have more clear wording. – Woot4Moo Oct 31 '12 at 21:03