Initializing Variables Tutorial

In Java, a variable is also called a field. I have been programming since the mid 90's and I have used the term field thousands of times with relation to databases. When it comes to variables, I don't use the term field, I refer to Java fields as variables or identifiers. Just be aware of the terminology when you run across the term field. As you have learned in previous tutorials, a declared variable is assigned a literal value using the assignment operator (=). When you declare a variable and assign it a value in the same statement, that process is known as initializing a variable.

  • (data type) (variable); // declaration statement
  • (variable) (assignment operator = ) (literal); // assignment statement
  • (data type) (variable) (assignment operator = ) (literal or expression); // initializing a variable
There are many legal ways to initialize variables in Java. This tutorial will go over some of them.

Default Values

When you type a simple declaration statement with no assignment, variables are assigned the following default values:

Data Type               Variable Value
byte                    0
short                   0
int                     0
long                    0L
char                    '\u0000'
double                  0.0D
float                   0.0F
boolean                 false

objects (like String)   null  

Special note - the compiler will let you declare and not initialize a variable. However, if you try to use that variable before you have assigned it a value, the compiler will error out. Not initializing variables and relying on default values is not a good programming practice.

Dynamic Initialization

In Java, you can initialize a variable without assigning a literal value. You can assign the value of a variable with a compatible expression at the time the variable is declared

  • double hourlyWage = 15.75; // initializing a variable
  • double hoursWorked = 40; // initializing a variable
  • double weeklyPay = hoursWorked * hourlyWage; // dynamic initialization



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Initializing
C:\Java>cd Initializing
C:\Java\Initializing>Notepad Initializing.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Initializing {
    public static void main(String args[]) {
        int a; // declaration statement
        a = 5; // assignment statement
        int b = 10; // initializing a variable
        boolean likesFish, likesVegetables = true, likesCoffee; // only likesVegetables = true is initialized
        String emptyString; // default value is null
        double hourlyWage = 15.75, hoursWorked = 41; // initializing variables
        double weeklyPay = hoursWorked * hourlyWage; // dynamic initialization

        System.out.println(a);
        System.out.println(b);
        //System.out.println(likesFish); // using variable before assigning a value - error. Uncomment to see error
        System.out.println(likesVegetables);
        //System.out.println(likesCoffee); // using variable before assigning a value - error. Uncomment to see error
        //System.out.println(emptyString); // using variable before assigning a value - error. Uncomment to see error
        System.out.println(weeklyPay);
    }
}

Now switch back to the command prompt (CMD) and type in javac Initializing.java and press Enter.
Now type in java Initializing and press Enter.


C:\Java\Initializing>javac Initializing.java
C:\Java\Initializing>java Initializing
5
10
true
645.75


Final thoughts

Initializing variables is a good programming practice.
Simple declaration statements relying on default values is not a good programming practice.


Tutorials