4

When declaring primitives/objects, are them initialized?

Which are the default value?

What is the behavior on class members and local fields?

What about objects declaration on class members?


As answered below, these are the default values:

Data Type - Default Value (for fields)

byte  0
short 0
int   0
long  0L
float 0.0f
double    0.0d
char  '\u0000'
String (or any object)    null
boolean   false

Please note that objects are initialized as null

2
  • 1
    It will be 0 in JavaSE as well Commented Dec 20, 2012 at 12:52
  • The first code the int is a class member and second its a local variable, that is the difference Commented Dec 20, 2012 at 12:56

1 Answer 1

5

The default value of int is 0 and that is the value it will have in both JavaSE and JavaEE unless it was assigned with another value.

You can't have an uninitialized int class member in Java (or any other primitive).

In your example you show the int is a class member, in the other example its a local variable, that is the difference.

For class members JVM will put the default values, for a local variables it makes you assign and initial value before accessing the variable.

You can check the Default Values section in Primitive Data Types for more information about the class members default values.

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

5 Comments

Does JVM calls the default constructor for objects class members , as well?
No, its just for primitives, see here (Default Values)
I must add, that all objects are initialized by default as null
Ah you are right sorry, I mixed up null and uninitialized for a second.
float[] hello = new float[1] // Why this initialization is required. May I get some help please.

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.