1

In the constructor below, I have initialized only two of the variables, leaving some variables uninitialized explicitly.

As i have read, no argument constructor is created by the compiler if the constructor is provided by us.Then in such cases, since I have my own constructor so there is no default constructor which initializes variables p and q.

So, the logic should be if we try to access those uninitialized variables, then it should be a compile time error.However, the following code runs successfully.

The output is 5 10 0.0 0.0

How can we explain the output 0.0 and 0.0 since i have not declared them in the constructor ??

public class Rectangle {
int l, b;
double p, q;

public Rectangle(int x, int y) {
l = x;
b = y;
}

public static void main(String[] args) {
    Rectangle obj1= new Rectangle(5,10);
    System.out.println(obj1.l);
    System.out.println(obj1.b);
    System.out.println(obj1.p);
    System.out.println(obj1.q); 
}

}

5 Answers 5

4

Primitives are initialized to a default value, in this case, 0 for integers and 0.0 for floats or doubles. Objects are by default null, for example a String would be null.

The values you set in constructors are normally different from the default values, therefore the need for constructors.

The default initial values are a language feature, and does not require a constructor at all.

You read about the default values here : http://www.janeg.ca/scjp/lang/defaults.html

Remember though that the default values are only for CLASS members, not local variables. That difference is why you get compile errors for some values not being initialized.

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

3 Comments

So does it mean that primitive data types are not initialized to their default value by the default no argument constructor ??
The default initial values are a language feature ??
The default constructor doesn't do a thing to member variables, those are initialized by Java for you. Your application is already proof of that. It's when you want different values that a constructor comes into play. It's confusing in the beginning, but you'll soon understand the pattern.
0

simple datatypes cannot be null (types like int or double) only objects can be null. the default value of those numerical simple data types is always 0. so if you do'nt initialize your double values you'll always get 0.0 if you want to use objects use the wrapper classes Integer or Double

Comments

0

The rules are different for local variables and for everything else. Fundamentally, the difference is between stack allocation and heap allocation. All memory allocated on the heap (and that's all instances and class vars) is zeroed out before your code getting a chance to observe it, unlike the stack frame. Therefore Java ensures by static analysis that your code cannot read a local var before initializing it, but there is no such check for a heap-allocated var, which will always have a consistent value (whetever is the interpretation of zero bytes by the type in quetion -- zero numbers, false booleans, null-refs).

Comments

0

By default java initializes double to 0.0

This because double is a primitive datatype which cannot be the null reference.

From java documentation:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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

2 Comments

but isn't it only when we are not providing ANY CONSTRUCTOR !!
docs.oracle.com/javase/specs/jls/se5.0/html/… - class & instance variables are initialized
0

Remember this,

In Java the instance variables are initialized by default to their respective default values. Thats the reason your uninitialized values are 0.0 (as their data types are double)

char = \u0000

double = 0.0

object reference variable = null;

But in Java the local variables (ie inside the methods) must be initialized before they are used. And that must be done by the programmer.

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.