Stop 02 · Variables, Data Types & OperatorsTopics · 1/7

Variables & Primitive Data Types

Part of the Java Roadmap

In short

Every program juggles values: a price, an age, a yes or no. In Java, each value lives in a variable with a fixed type, and choosing the right type is your first real design decision.

Java is statically typed. Its 8 primitive types are byte, short, int, long, float, double, char and boolean, each with a fixed size. var lets the compiler infer local variable types.

The jaadu lines up steel containers from tiny to huge on a kitchen counter and carefully pours grains into the right-sized one.

Think of it like this

Steel dabbas of fixed sizes

A kitchen has a small dabba for salt, a medium one for dal and a big drum for rice. You cannot pour rice into the salt box, and each box is labelled. A Java variable is a labelled box with a fixed size and kind: an int box holds whole numbers, a boolean box holds only true or false.

Watch it happen

The eight primitive types, smallest to largest

boolean
0
byte 8-bit
1
char 16-bit
2
short 16-bit
3
int 32-bit
4
float 32-bit
5
long 64-bit
6
double 64-bit
7

1/5Java has exactly eight primitive types. Everything else, like String, is an object.

The big ideas

What you need to know

Statically typed

A variable's type is fixed when you declare it. int age = 21; can never later hold "twenty one".

int and long

int goes up to about 2.1 billion. For bigger values, like phone numbers or milliseconds, use long and add L: 9876543210L.

double for decimals

double has about 15 digits of precision. For exact money maths in real systems, use BigDecimal.

Declare, then assign

int count; declares. count = 5; assigns. Local variables must be assigned before you read them.

See it in code

Walk through the code

java
1public class Student {2    public static void main(String[] args) {3        int age = 20;4        long mobile = 9876543210L;5        double cgpa = 8.45;6        char grade = 'A';7        boolean hostelite = true;8        String name = "Ananya";9 10        System.out.println(name + ", " + age + ", CGPA " + cgpa);11        System.out.println("Grade " + grade + ", hostel: " + hostelite);12        System.out.println("Max int: " + Integer.MAX_VALUE);13    }14}

1/5int for a small whole number.

Five primitives and one String describing a student. Integer.MAX_VALUE shows the int ceiling.

Save yourself the pain

Common mistakes

Oops

int phone = 9876543210;

Do this instead

That number is bigger than an int can hold, so it will not compile. Use long phone = 9876543210L; or better, a String, since you never do maths on phone numbers.

Oops

float price = 99.5;

Do this instead

Decimal literals are double by default. Write float price = 99.5f; or just use double.

Your mission

Describe your phone

~15 min

Quick check

Which of these is NOT a primitive type in Java?

Go deeper

  1. 1Declare one variable of each primitive type and print it.
  2. 2Find the maximum value of int with Integer.MAX_VALUE and see what happens when you add 1.
  3. 3Learn when to use long instead of int and double instead of float.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge