16

I have a confusion about variable initialization in Java. As I understand, class variables get default initialization while local variables are not initialized by default. However, if I create an array inside a method using the new keyword, it does get initialized by default. Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

2
  • 1
    Did you try it out to see what happens? Commented Mar 1, 2013 at 19:22
  • Yeah, I tried with other objects and that's what seems to happen, which is why I asked the question Commented Mar 1, 2013 at 19:24

3 Answers 3

43

From Java Language Specification

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

  • 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

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for actually listing all the primitive types, I was too lazy =).
Thanks I know what the initialized values are when they are initialized. I was just not sure in what situations objects are default initialized.
6

After further investigation, primitives will always initialize to the default only when they are member variables, local variables will throw a compile error if they are not initialized.

If you create an array of primitives they will all be initialized by default (this is true for both local and member arrays), an array of objects you will need to instantiate each one.

6 Comments

I am not sure of this. When I declare a primitive inside a method, it does not get initialized by default. If I try to use it without initialization, I get a compilation error.
yes, compilers will spit out "this variable may not have been initialized" messages for uninitialized local primitives
@JeffHawthorne really? I don't have a java compiler in front of me to test this, but I thought primitives would always be given default values when declared, regardless of where.
@BM I did a little extra testing on this, everything I said was true except I wasn't aware of the compile error when using an uninitialized local variable. Edited my answer.
@KevinDiTraglia i'm not sure if it's a warning or an error (some of these vary from IDE to IDE) but i've had that happen in methods when i tried to just do int i; or something like that
|
4

Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

When you use new keyword. it means that you have initialized your Object. doesn't matter if its declared at method level or instance level.

public void method(){
Object obj1;// not initialized
Object obj2 = new Object();//initialized
}

Comments

Your Answer

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.