Skip to content
Educora
Beginner15 min2 / 18

Variables, types and operators

Learn to declare variables and get to know Java's eight primitive types, arithmetic operators and type casting.

Check yourself
In this lesson you will learn
  • Declare a variable with a type and assign a value to it
  • Choose a suitable primitive type for a task
  • Calculate correctly with integer division, remainders and type casting

An online shop has to remember each product's name, its price, how many are left in stock and whether it is on sale. Programs keep such information in variables. In Java every variable has a type that is known in advance and never changes — languages like this are called statically typed.

Declaring variables

To create a variable, write its type first, then its name, and optionally give it a value right away with =: int age = 14;. Later you can assign a new value, but not a value of a different type: you cannot put text into an int variable.

Java
public class Main {
    public static void main(String[] args) {
        String name = "Leyla";
        int age = 14;
        double height = 1.62;
        boolean isStudent = true;
        char grade = 'A';
        System.out.println(name + " is " + age + " years old.");
        System.out.println("Height: " + height + " m");
        System.out.println("Student: " + isStudent + ", grade: " + grade);
        age = age + 1;
        System.out.println("Next year: " + age);
    }
}
Expected output
Leyla is 14 years old.
Height: 1.62 m
Student: true, grade: A
Next year: 15

Primitive types

Java has eight primitive types — the simplest building blocks of the language. They differ in how much memory they take and which values they can hold. For whole numbers you usually choose int, and for decimals double.

TypeSizeWhat it holdsExample
byte8 bitswhole numbers from −128 to 127byte level = 5;
short16 bitswhole numbers from −32,768 to 32,767short year = 2026;
int32 bitsabout ±2.1 billionint people = 10_000_000;
long64 bitsabout ±9.2 · 10¹⁸long stars = 3_000_000_000L;
float32 bitsa decimal, about 7 digits of precisionfloat t = 36.6f;
double64 bitsa decimal, about 15–16 digits of precisiondouble price = 9.99;
char16 bitsa single character (Unicode)char grade = 'A';
boolean–true or falseboolean ok = true;
A long literal ends with L and a float literal with f. Underscores _ make big numbers easier to read.

String is not in this list: it is not a primitive type but a class — that is why it starts with a capital letter. You will get to know strings properly in the next lesson. Note that a char value goes in single quotes ('A'), while a String uses double quotes.

Interactive
Loading simulation…
A byte is made of 8 bits. Toggle the bits: 8 bits give 256 different combinations, and Java uses them for the numbers from −128 to 127.

Arithmetic operators

Java has the operators +, -, *, / and % (the remainder after division). The most important detail: when you divide two integers, the result is also an integer — the fractional part is simply dropped. So 17 / 5 is 3, not 3.4.

Java
public class Main {
    public static void main(String[] args) {
        int a = 17;
        int b = 5;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.println(a % b);
        System.out.println(17.0 / 5);
        int count = 10;
        count += 5;
        count++;
        System.out.println(count);
    }
}
Expected output
22
12
85
3
2
3.4
16

As soon as one of the numbers is a decimal (17.0), the result is a decimal too. count += 5 is a short way of writing count = count + 5; -=, *= and /= work the same way. count++ increases a variable by 1, and count-- decreases it by 1.

Example: minutes to hours

A film lasts 135 minutes. How can you show this in hours and minutes?

Show solution
Hours — integer division: 135 / 60 = 2.
Remaining minutes — the remainder: 135 % 60 = 15.
System.out.println(135 / 60 + " h " + 135 % 60 + " min"); prints 2 h 15 min.

Type casting

Sometimes you need to turn a value of one type into another. From a smaller type to a bigger one (widening, e.g. int → double) Java converts automatically and safely. From a bigger type to a smaller one (narrowing, e.g. double → int) information can be lost, so you must ask for it explicitly by writing the type in parentheses: (int) price.

Java
public class Main {
    public static void main(String[] args) {
        int apples = 7;
        double d = apples;
        System.out.println(d);
        double price = 9.99;
        int whole = (int) price;
        System.out.println(whole);
        int sum = 7, people = 2;
        System.out.println(sum / people);
        System.out.println((double) sum / people);
        System.out.println((int) 'A');
        System.out.println((char) 66);
        int max = Integer.MAX_VALUE;
        System.out.println(max + 1);
    }
}
Expected output
7.0
9
3
3.5
65
B
-2147483648
  • (int) 9.99 gives 9: the fractional part is dropped, not rounded.
  • (double) sum / people first turns sum into a double, so the division keeps the fraction: 3.5.
  • A char is really a number: the code of 'A' is 65, and 66 is 'B'.
  • Adding 1 to the largest int causes an overflow: the value “wraps around” to the smallest negative number.

Key points

  • Java is statically typed: a variable is declared as type name = value; and its type never changes.
  • There are eight primitive types: byte, short, int, long, float, double, char, boolean; String is a class.
  • Dividing two integers gives an integer: 17 / 5 = 3, 17 % 5 = 2.
  • Widening is automatic; narrowing needs an explicit cast: (int) 9.99 = 9.
  • final creates a constant, and var lets the compiler work out the type for you.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What does System.out.println(17 / 5); print?