Utilizor
Contact Us

Java Variables

Storing data in variables.

Examples

Create String Variable

Creating a variable of type String.

public class Main {
  public static void main(String[] args) {
    String name = "John";
    System.out.println(name);
  }
}

Create Integer Variable

Creating a variable of type int.

public class Main {
  public static void main(String[] args) {
    int myNum = 15;
    System.out.println(myNum);
  }
}

Change Variable Value

Reassigning a value to a variable.

public class Main {
  public static void main(String[] args) {
    int myNum = 15;
    myNum = 20;  // myNum is now 20
    System.out.println(myNum);
  }
}

Final Variables

Using 'final' to create constants.

public class Main {
  public static void main(String[] args) {
    final int myNum = 15;
    // myNum = 20;  // will generate an error: cannot assign a value to a final variable
    System.out.println(myNum);
  }
}

Test Your Knowledge

Java Quiz

No quiz available for this topic yet.