Java Variables
Name a value. Use it with purpose.
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.
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.
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 quotesint– stores integers (whole numbers), without decimals, such as 123 or -123float– stores floating point numbers, with decimals, such as 19.99 or -19.99char– stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean– stores values with two states: true or false
Declaring (Creating) Variables
To create a variable in Java, you need to:
- Choose a type (like
intorString) - Give the variable a name (like
x,age, orname) - Optionally assign it a value using
=
Here's the basic syntax:
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
int | The data type. It tells Java this variable will hold a whole number (integer). |
myAge | The 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. |
25 | The 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.
// 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 100A 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.
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.
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.
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.
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.