Friday, 10 August 2007

Operators and Assignments (Part 3)

Variable Initialization

All variables in Java have to be initialized before you can use them in your Code. When an object is created, instance variables are automatically initialized when the storage is allocated. These instance variables are initialized to either 0 or null (booleans are initialized to false).

Local variables, on the other hand, have to be explicitly declared. Consider the following Code snippet:

public void computeSum(){
int x = 100;
int y;
int z;
// this will cause an error during compilation
z = y + x;
}

No comments: