codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java variables

Java variables

Every variable has a type.

Declare a variable with its type: int, double, char, boolean or String. Add final to make a constant that cannot change. Since Java 10 var lets the compiler infer the type of a local variable.

Example code

public class Main {
  public static void main(String[] args) {
    String name = "Ada";
    int age = 20;
    double price = 4.5;
    char grade = 'A';
    boolean ok = true;
    final int MAX = 10;
    var city = "Delhi";
    System.out.println(name + " " + age + " " + price + " " + grade + " " + ok + " " + MAX + " " + city);
  }
}

Run this code in the online editor

← Java commentsJava data types →