Lesson 2 of 5 15 min read
Variables, primitive data types and operators
Store values in variables, choose the right primitive type and calculate with arithmetic, assignment and comparison operators.
In this lesson you will
- Declare, initialise and update variables
- Choose an appropriate primitive type for a value
- Use arithmetic, assignment and comparison operators and predict their results
Variables are labelled boxes
A variable is a named location in memory that holds a value. In Java you declare a variable with a type and a name, and usually give it a starting value:
int age = 21;
double price = 499.50;
boolean isEnrolled = false;
char grade = 'A';
String name = "Priya";age (int) holds 21; price (double) holds 499.50; isEnrolled (boolean) holds false; grade (char) holds 'A'; name (String) holds "Priya"
The type tells Java what kind of data the variable holds and how much memory it needs. Java is statically typed: once age is an int, it can only hold whole numbers.
You can change a variable's value later with the assignment operator =:
int score = 10;
score = 15; // replaces 10 with 15
score = score + 5; // reads 15, adds 5, stores 20The eight primitive types
| Type | Holds | Example |
|---|---|---|
byte | Whole numbers from -128 to 127 | byte level = 3; |
short | Whole numbers from -32,768 to 32,767 | short year = 2026; |
int | Whole numbers, about ±2.1 billion | int students = 120; |
long | Very large whole numbers | long views = 9_000_000_000L; |
float | Decimal numbers, less precise | float ratio = 0.75f; |
double | Decimal numbers, the default choice | double gpa = 8.6; |
char | A single character | char initial = 'D'; |
boolean | true or false | boolean passed = true; |
For most programs, int for whole numbers, double for decimals and boolean for yes/no are enough. String is not a primitive: it is a class for text, written in double quotes.
Arithmetic operators
int a = 17;
int b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3 (integer division drops the decimal part)
System.out.println(a % b); // 2 (remainder)
System.out.println(17.0 / 5); // 3.4 (one double operand gives a double result)Integer division surprises many beginners: when both operands are int, the result is an int and the fractional part is discarded. Make at least one operand a double to keep decimals.
The remainder operator % is very useful. For example, n % 2 == 0 tells you whether n is even.
Assignment shortcuts
int count = 0;
count += 5; // same as count = count + 5 -> 5
count -= 2; // same as count = count - 2 -> 3
count *= 4; // -> 12
count++; // add one -> 13
count--; // subtract one -> 12Comparison operators
Comparisons produce a boolean:
| Operator | Meaning | Example (x = 7) |
|---|---|---|
== | equal to | x == 7 is true |
!= | not equal to | x != 7 is false |
> / < | greater / less than | x > 10 is false |
>= / <= | greater / less than or equal | x <= 7 is true |
Note the difference: = assigns a value, while == compares two values.
Putting it together
public class Bill {
public static void main(String[] args) {
double mealCost = 850.0;
int people = 4;
double tipPercent = 10;
double total = mealCost + mealCost * tipPercent / 100;
double perPerson = total / people;
System.out.println("Total: " + total); // Total: 935.0
System.out.println("Each pays: " + perPerson); // Each pays: 233.75
}
}The + operator also joins text: "Total: " + total builds a single String.
Hands-on exercise
Write a program that stores the length and width of a rectangle (in metres) as double variables, then prints its area and perimeter. Next, store a number of minutes as an int (for example 135) and print it as hours and minutes ("2 hours 15 minutes") using / and %.
Knowledge check
3 questions • 70% to pass • Practice only, no certificate
Key concepts
- A variable has a type, a name and a value; its type cannot change.
- Use int for whole numbers, double for decimals, boolean for true/false and String for text.
- Integer division drops the fractional part; % gives the remainder.
- = assigns a value, == compares two values and produces a boolean.
