Java Unassigned Variables

Java Unassigned Variables


Unassigned Local and Instance Variables

Summary:

In this "Java unassigned variables" tutorial, you will learn Java nature of unassigned variables which quiet contract to C/C++.

One of the features of Java is "Java does not support garbage values". If a variable is used without a value assigned, it is either compilation error or takes a default value. If a local variable is not given a value and still used in the program it is a compilation error. But in case of an instance variable, it takes a default value. For example, an integer variable takes 0 by default. A novice should remember the nature of this Java unassigned variables.

Following program illustrates.

Java Unassigned Variables

Output screen of Values.java of Java Unassigned Variables

In the above code, price, rate and raining are unassigned instance variables and printed default values. But unassigned local variable marks is placed in comments. If comments are removed and compiled the program, it raises compilation error.

The following table gives the default values for Java unassigned variables (instance variables)

.

Date type Default value
byte
0
short
0
int
0
long
0
float
0.0
double
0.0
char
\u0000 (does not print any value)
boolean
false

On Java unassigned variables, more discussion is available at Data Types Default Values – No Garbage.

Explanation for calling Java unassigned variables from methods is discussed in Using Variables from Methods.

Leave a Reply

Your email address will not be published. Required fields are marked *