Skip to lesson
MentorNxt home

Java Programming

Variables & Data Types

What is a variable?

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.

int marks = 85; has a type (int), a name (marks), = to store, a value (85) and a semicolon. It makes a box in memory named marks that holds 85.

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;.

Syntax
type variableName = value;

Example 1 · Declare first, assign later

Attendance.javaJava
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);    }}
Output
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

Marks.javaJava
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);    }}
Output
85
marks
Marks: 85

What this code does

  • marks without quotes prints the value in the box, 85.
  • "marks" with quotes is text, so it prints the word itself.
  • "Marks: " + marks joins 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

public class Scores {
public static void main(String[] args) {
int runs;
runs = 45;
int bestScore = runs;
runs = 72;
System.out.println("Runs today: " + runs);
System.out.println("Old best: " + bestScore);
}
}

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.

Step 1 of 6

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: marks2 is fine, 2marks is not.
  • No spaces: write totalMarks, not total marks.
  • Do not use Java's own words, called keywords, such as int, class or public.
  • Capital letters matter: marks and Marks are two different names.
Pick names that say what the value is. attendance is clear; a or x1 is not.

Next step

Check your understanding

8 quick questions