What is a variable?
A variable is a labelled box in the computer's memory. You put a value in the box, give the box a name, and later refer to the value by its name. Variables let you store user input, intermediate results and program state without hard-coding numbers and strings everywhere.
Declaring a variable
In Java you declare a variable with the syntax shown below. Choose names that describe what the value represents: userAge is far better than x. Stick to one naming style across a project (camelCase, snake_case…) so the code stays consistent.
int age = 25;
String name = "Aman";
boolean isStudent = true;
System.out.println(name + " " + age);Reassignment and scope
Most variables can be reassigned later — the box stays, the contents change. The places in your code where a variable is visible is called its scope. Variables declared inside a function are usually only visible inside that function, which keeps programs predictable as they grow.
Details for java
Variables have declared types. Locals must be assigned before use; fields have defaults. final prevents reassignment, not mutation of referenced objects. Integer division 5 / 2 yields 2; 5 / 2.0 yields 2.5.
Practice and verify
Change one meaningful value in the example and predict the result before running. Then test a boundary or empty value. Explain what changed and why, rather than only copying the output.