Lesson 2 of 514 minModule progress 0%

Module 2: Output, Input, Variables, and Types

Variables and Naming Rules

Store values in named variables so your program can reuse and update information instead of hardcoding everything.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-16

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, edited by hand, and checked against current Java 25 documentation and runnable examples.

Learning goals

  • Declare a variable with a type and name
  • Assign and update values
  • Use clear beginner-friendly variable names

What a variable is: A variable stores a value so your program can use it later. In Java, every variable has a type and a name.

Basic shape: int age = 20; means the variable is named age, its type is int, and its value starts at 20.

Naming rules: Variable names cannot start with a number, and Java is case-sensitive. age, Age, and AGE are different names.

Naming style: Use descriptive names like studentCount or price, not vague names like x unless the example is tiny and temporary.

Runnable examples

Declare and print variables

public class Main {
    public static void main(String[] args) {
        String city = "Berlin";
        int year = 2026;
        System.out.println(city);
        System.out.println(year);
    }
}

Expected output

Berlin
2026

Update a variable

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

Expected output

15

Common mistakes

Choosing unclear names like `a`, `b`, or `temp2` in beginner exercises

Use names that describe the value, such as `userName` or `totalPrice`.

Assuming Java ignores uppercase and lowercase

Remember that `score` and `Score` are different variable names.

Mini exercise

Create variables for your name, age, and favorite number, then print all three.

Summary

  • Variables store values for later use.
  • Java variables need a declared type.
  • Clear names make beginner code much easier to read.

Next step

With variables in place, learn the primitive types you will use most often.

Sources used

Advertisement

Lesson check

Which variable name is valid in Java?

Next lesson →