What is a variable?
16 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A variable is a name for a value your program needs to remember, such as your marks or your UPI balance.
A variable is a named box
Your program needs your marks, 85, in three places, and typing 85 each time is easy to get wrong. A variable is a name for one place in memory (RAM, the computer's short-term storage) that holds one value, like a labelled box.
Declare, assign and initialize
Sometimes you know a value only later, for example after an exam is marked. Declaring makes the box (int marks;), assigning puts a value in (marks = 85;), and initializing does both in one line: int marks = 85;.
Example 1 · Declare first, assign later
public class Attendance { public static void main(String[] args) { int classesHeld; int classesAttended; classesHeld = 40; classesAttended = 36; System.out.println("Classes held: " + classesHeld); System.out.println("Classes attended: " + classesAttended); }}Classes held: 40 Classes attended: 36
What this code does
int classesHeld;only declares the variable: the box exists, but it has no value yet.classesHeld = 40;assigns a value.=means "put the value on the right into the variable on the left", not "is equal to" as in maths.- Only after the assignment can the variable be printed.
A variable made inside main is a local variable. Java will not let you use it before it has a value: printing classesHeld before line 5 does not compile.
Print, change and copy
You stored 85 in marks, and now you want to see it on screen with a label. Write the name without quotes to get the value; with quotes it is just a word.
Example 2 · The value or the word
public class Marks { public static void main(String[] args) { int marks = 85; System.out.println(marks); System.out.println("marks"); System.out.println("Marks: " + marks); }}85 marks Marks: 85
What this code does
markswithout quotes prints the value in the box, 85."marks"with quotes is text, so it prints the word itself."Marks: " + marksjoins the label and the value into one line.
To change the value, assign again without the type: marks = 92;. The old value is gone. You can also copy one variable into another: int bestScore = runs; gives the new variable its own copy. Step through the program below and watch both boxes.
Trace the code
Watch the boxes
Variables
None yet.
Output
(nothing printed yet)Line 3Declare: Java makes a box named runs. It has no value yet, so it cannot be printed.
Naming rules and camelCase
total marks looks like a good name, but Java reads the space as the end of the name, so the line does not compile. Names follow a few strict rules, and a style called camelCase: start with a small letter and start every new word with a capital, like totalMarks.
- Use English letters, digits,
_and$. Do not start with a digit:marks2is fine,2marksis not. - No spaces: write
totalMarks, nottotal marks. - Do not use Java's own words, called keywords, such as
int,classorpublic. - Capital letters matter:
marksandMarksare two different names.
attendance is clear; a or x1 is not.Next step
Check your understanding
8 quick questions