Java Variables

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 06 / 50 · English

Java Variables

Name a value. Use it with purpose.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Variables.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

A variable associates a name and type with a value. Start with meaningful names such as lessonsCompleted. Assigning a new value changes what the variable holds; it does not change its declared type. Local variables must be assigned before you read them.

02 / Step by step

Explore the details

Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

  • String – stores text, such as "Hello". String values are surrounded by double quotes
  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • float – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • boolean – stores values with two states: true or false

Declaring (Creating) Variables

To create a variable in Java, you need to:

  • Choose a type (like int or String)
  • Give the variable a name (like x, age, or name)
  • Optionally assign it a value using =

Here's the basic syntax:

Code anatomy / கோடின் விளக்கம்

CodeMeaning
intThe data type. It tells Java this variable will hold a whole number (integer).
myAgeThe variable name. You choose this. It should describe what the value represents. Java uses camelCase by convention.
=The assignment operator. It assigns the value on the right to the variable on the left.
25The value being stored in the variable. Since the type is int, this must be a whole number (no decimals).
;The semicolon ends the statement. Every Java statement must end with a semicolon.

For example, if you want to store some text, you can use a String:

To create a variable that should store a number, you can use int:

You can also declare a variable without assigning the value, and assign the value later:

Note that if you assign a new value to an existing variable, it will overwrite the previous value:

Final Variables

If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will declare the variable as "final" or "constant", which means unchangeable and read-only):

Other Types

A demonstration of how to declare variables of other types:

You will learn more about data types in the next section.

Java reference fragmentJAVA
// Declare then assign separately
int age;
age = 25;

// Declare and initialize together
String name = "Alice";
double salary = 45000.50;
boolean isActive = true;
char grade = 'A';

// Multiple vars of same type
int x = 10, y = 20, z = 30;

// var keyword (Java 10+) — type inferred
var city = "Chennai";   // String
var count = 100;         // int
System.out.println(city + " " + count);  // Chennai 100
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        String learner = "Kavi";
        int lessonsCompleted = 2;
        lessonsCompleted = lessonsCompleted + 1;
        System.out.println(learner + " completed " + lessonsCompleted + " lessons.");
    }
}
Predict first. Then reveal the expected output / வெளியீடு
Kavi completed 3 lessons.

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Start the count at 5 and increase it by 2.

Reveal the answer and reasoning / விடை

The final count becomes 7. Change the initialization and the value added in the assignment.

One mistake to avoid: The = symbol assigns a value. It does not ask whether two values are equal.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.