Java Variables

A variable is a named box in the memory where you keep a value. In Java every variable has a type, which says what kind of value it can keep.

Declare and assign

Syntax: type name = value;

public class Main {
    public static void main(String[] args) {
        int age = 21;
        double price = 99.5;
        String name = "Riya";
        boolean active = true;

        System.out.println(name + " is " + age + " years old");
        System.out.println("Price: " + price + ", active: " + active);
    }
}
Output

Riya is 21 years old
Price: 99.5, active: true

Declare first, assign later

public class Later {
    public static void main(String[] args) {
        int score;          // declared
        score = 50;         // assigned
        score = score + 10; // changed
        System.out.println(score);
    }
}
Output

60

A local variable must have a value before you use it. If not, the compiler gives an error:

public class Empty {
    public static void main(String[] args) {
        int x;
        System.out.println(x);
    }
}
Output

Empty.java:4: error: variable x might not have been initialized
System.out.println(x);
^
1 error
error: compilation failed

Many variables of one type

public class Many {
    public static void main(String[] args) {
        int a = 1, b = 2, c = 3;
        int x, y;
        x = y = 10;
        System.out.println(a + b + c + " " + x + " " + y);
    }
}
Output

6 10 10

Constants with final

Add final and the value can not be changed. Constants are written in UPPER_CASE.

public class Const {
    public static void main(String[] args) {
        final double PI = 3.14159;
        PI = 3;
    }
}
Output

Const.java:4: error: cannot assign a value to final variable PI
PI = 3;
^
1 error
error: compilation failed

The var keyword

Since Java 10 you can write var for local variables, and Java finds the type from the value. The variable is still strongly typed.

public class Var {
    public static void main(String[] args) {
        var count = 10;          // int
        var title = "Java";      // String
        var rate = 4.5;          // double
        System.out.println(count + " " + title + " " + rate);
    }
}
Output

10 Java 4.5

Naming rules

  • Start with a letter, _ or $, not with a digit.
  • Use camelCase: totalPrice, studentName.
  • Use meaningful names: age is better than a.
  • You cannot use a keyword as a name.

Scope

A variable lives only inside the block { } where you declared it.

public class Scope {
    public static void main(String[] args) {
        if (true) {
            int inside = 5;
        }
        System.out.println(inside);
    }
}
Output

Scope.java:6: error: cannot find symbol
System.out.println(inside);
^
symbol: variable inside
location: class Scope
1 error
error: compilation failed

Note: Give a variable the smallest scope you can, and declare it near the place where you first use it. Next: Java Data Types.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.