Chitransh Saurabh
Chitransh Saurabh

Reputation: 377

Invoking uninitialized variables in constructors

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); 
}

}

Upvotes: 1

Views: 2624

Answers (5)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

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.

Upvotes: 0

tgoossens
tgoossens

Reputation: 9856

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

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200148

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

Upvotes: 0

tagtraeumer
tagtraeumer

Reputation: 1491

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

Upvotes: 0

Ewald
Ewald

Reputation: 5751

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.

Upvotes: 4

Related Questions