onepiece
onepiece

Reputation: 3539

Empty arrays automatically initialize contents?

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

Upvotes: 1

Views: 9567

Answers (5)

kosa
kosa

Reputation: 66657

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.

Upvotes: 4

Code-Apprentice
Code-Apprentice

Reputation: 83557

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.

Upvotes: 2

Robert Cooper
Robert Cooper

Reputation: 1270

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.

Upvotes: 6

Woot4Moo
Woot4Moo

Reputation: 24336

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.

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34367

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.

Upvotes: 2

Related Questions