Primitive data types (int, double, char, boolean…)

A variable's type decides how many bits of memory it gets and how those bits are read. Java has exactly eight built-in primitive types. In this lesson you'll learn their sizes and ranges, how to write their values, and why Integer.MAX_VALUE + 1 is negative and 0.1 + 0.2 isn't exactly 0.3.

Module 2 · Language Basics⏱ 18 minBeginnerNo install needed8 practice problems
1 The story

The marks sheet that broke

Riya is building a marks sheet for her class: age, roll number, percentage, grade and pass/fail. She stores everything as int. The percentage 87.5 turns into an error, and the grade 'A' won't fit either. She asks Arjun Sir which type to use for what.

20 byteage · 8 bits 45000 intsalary · 32 bits 1400000000 longpopulation · 64 bits 87.5 doublepercentage · 64 bits 'A' char true boolean myphatshala.in
Riya

Sir, can't I just use int for everything? It's easier.

Arjun Sir

An int box only holds whole numbers, and only up to about 2.1 billion. 87.5 has a decimal part, 'A' is a character, and India's population needs a bigger box. Java gives you eight sizes and shapes of box, the primitive types.

Riya

Why not one giant box for everything?

Arjun Sir

Memory and speed. A college system might store crores of marks. Using 1 byte instead of 8 for each age saves a lot. Let's look inside each box, right down to the bits.

2 Definition

What are primitive data types?

Formal definition (write this in exams)

A data type specifies the kind of values a variable can hold, the memory it needs and the operations allowed on it. Java has eight primitive data types, built into the language and not objects: four integer types (byte, short, int, long), two floating-point types (float, double), one character type (char) and one logical type (boolean). Their sizes are fixed by the language on every platform.

In simple words

Primitives are Java's basic building blocks: whole numbers in four sizes, decimal numbers in two precisions, a single character, and true/false. They hold the value directly in the box, unlike objects (next lesson), where the box holds an arrow.

Words you'll meet in this lesson

Bit / byteA bit is a 0 or 1. A byte is 8 bits.
RangeThe smallest and largest value a type can hold.
LiteralA value written directly in code: 42, 19.5, 'A', true.
OverflowGoing past the maximum. Java integers silently wrap around to the minimum.
PrecisionHow many digits a decimal type can keep exactly (float ~7, double ~15–16).
Two's complementThe way Java stores negative integers in binary; the leftmost bit is the sign.

The eight primitives

TypeSizeRange / valuesDefaultExample
byte8 bits−128 to 1270byte age = 20;
short16 bits−32,768 to 32,7670short year = 2026;
int32 bitsabout −2.1 billion to 2.1 billion (2,147,483,647)0int salary = 45_000;
long64 bitsabout ±9.2 × 10180Llong pop = 1_400_000_000L;
float32 bits±3.4 × 1038, about 7 digits0.0ffloat temp = 36.6f;
double64 bits±1.8 × 10308, about 15–16 digits0.0double pct = 87.5;
char16 bits0 to 65,535 (UTF-16 code unit)'\u0000'char grade = 'A';
booleannot fixed by the spectrue or falsefalseboolean passed = true;

Exam tip: sizes are the same on every platform (that's Java's “portable” feature). Default choices: int for whole numbers, double for decimals. A plain number like 42 is an int literal and 19.5 is a double literal, so long values beyond int's range need L and float values need f. A boolean is not 0 or 1 in Java: you can't write if (1).

3 Types and ways to use it

Four families, and how to write their values

Integers: byte, short, int, long

Whole numbers, positive or negative. Pick by range: int normally, long for big counts, IDs, timestamps in milliseconds or money in paise.

Exact

Floating point: float, double

Numbers with a decimal part, stored in binary scientific notation (IEEE 754). Very wide range, but approximate. Use double unless memory is very tight.

Approximate

Character: char

One 16-bit Unicode code unit, written in single quotes: 'A', '₹', '\n'. Internally it's a number, so 'A' + 1 is 66.

Unicode

Logical: boolean

Only true or false. The result of comparisons like marks >= 40, and the condition in every if and loop.

Yes / no

Writing literals

FormExampleMeaning
Decimal with underscores1_00_000One lakh. Underscores are only for readability (Java 7+).
Long suffix3_000_000_000LA long literal. Use capital L, since small l looks like 1.
Float suffix36.6fWithout f, 36.6 is a double and won't fit in a float.
Hexadecimal / binary / octal0xFF · 0b1010 · 017255 · 10 · 15 (a leading 0 means octal!)
Scientific notation1.5e31500.0
char forms'A' · 'A' · 65All the same character A
4 Step by step

Choosing the right type: a checklist

  1. Is it true/false?

    Use boolean: isPaid, hasDiscount, passed.

    The JVM usually stores a boolean field in a byte, and boolean arrays use one byte per element, but the language never lets you treat it as a number.
  2. Is it a single character?

    Use char: a grade, a gender code, a menu option. For words and sentences, use String (next lesson).

    'A' is stored as the number 65 in 16 bits: 0000 0000 0100 0001.
  3. Is it a whole number?

    Default to int. If it could exceed about 2.1 billion (bank balances in paise, file sizes, timestamps in milliseconds, IDs in big systems), use long.

    Arithmetic is done in at least 32 bits anyway: byte + byte produces an int. byte and short mainly save memory in large arrays.
  4. Does it have a decimal part?

    Use double for measurements: percentages, distances, temperatures.

    A double stores a sign bit, 11 exponent bits and 52 fraction bits. Most decimal fractions, like 0.1, have no exact binary form, so tiny rounding errors appear.
  5. Is it money?

    Don't use double. Store paise in a long, or use BigDecimal (Module 10).

    ₹0.10 + ₹0.20 as doubles gives 0.30000000000000004. In a bank, those tiny errors add up across millions of transactions.
5 See it happen

Inside the boxes: the bits behind each value

Left: the code and what it prints. Right: the actual bits in memory.

Primitive values, bit by bit 6 steps
What you see byte age = 100; char grade = 'A'; grade + 1 → ? int max = Integer.MAX_VALUE; max = max + 1; double sum = 0.1 + 0.2; boolean passed = marks >= 40; 100 66 (and (char)(grade + 1) is B) 2147483647 -2147483648 ← negative! 0.30000000000000004 true A small number in the smallest box. A char is secretly a number. The biggest number an int can hold. One more, and it jumps to the most negative. Not exactly 0.3! Comparisons produce booleans. No error, no warning: silent overflow. Tiny rounding error from binary fractions. myphatshala.in
Behind the screen byte: 8 bits 0 1 1 0 0 1 0 0 sign6432168421 64 + 32 + 4 = 100 The leftmost bit is the sign: 0 = positive. char: 16 bits, Unicode number 0000 0000 0100 0001 = 65 = 'A' 'A' + 1: both become int → 65 + 1 = 66. Code 66 is 'B'. '₹' is 8377, 'अ' is 2309. int: 32 bits 0111 1111 1111 1111 1111 1111 1111 1111 sign 0, all other bits 1 = 2,147,483,647 That's 2³¹ − 1, the largest positive int. 1000 0000 0000 0000 0000 0000 0000 0000 +1 carries into the sign bit → −2,147,483,648 Like a car odometer rolling over 99999 → 00000, but in two's complement the new sign bit means negative. double: 64 bits (IEEE 754) ± exponent (11) fraction (52 bits) 0.1 in binary = 0.000110011001100110011… (repeats forever) Only 52 bits fit, so it's cut and rounded. Two small errors added together become visible. Sizes compared byte short char int float long double booleantrue/false (size up to the JVM) myphatshala.in
6 Real-world use

Picking types in real systems

Wrong type choices are behind some famous software bugs. Here's how real systems choose.

Watch it with an example: when the box is too small

A video app counts views in an int. A shop adds prices in double. Both hit a surprise. Watch what happens to the bits, and the fix.

int overflow and double rounding 6 steps
Animated example int views = 2147483647; // Integer.MAX_VALUE views = views + 1; long views = 2147483647L + 1; double total = 0.1 + 0.2; double total = 0.1 + 0.2; long paise = 10 + 20; // money in paise int: 32 bits 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 = 2,147,483,647 (largest int) = −2,147,483,648 (wrapped around!) long: 64 bits 0000…0000 1000 0000 0000 … 0000 = 2,147,483,648 ✓ room to grow double: binary fractions 0.1 ≈ 0.1000000000000000055… 0.2 ≈ 0.2000000000000000111… prints 0.30000000000000004 long counts whole paise exactly 10 + 20 = 30 paise display: Rs 0.30 ✓ exact Pick the type by the size and kind of value. myphatshala.in
₹1,250.50125050 paise

1. Money in banking and payments

Payment systems commonly store amounts as whole paise in a long, or use BigDecimal, so every rupee adds up exactly. Floating-point types are avoided for money.

₹1,250.50→long amountInPaise = 125050→Exact totals
double

2. Maps, GPS and sensors

Latitude, longitude, speed and temperature are measurements with natural tiny errors anyway, so double is the right choice. Ride-hailing and delivery apps calculate distances with doubles.

GPS 19.0473, 73.0699→double lat, lng→Distance in km
1760500000000

3. Timestamps and IDs

System.currentTimeMillis() returns a long: milliseconds since 1 January 1970 are far beyond int's range. Order IDs and user IDs in large systems are also usually long.

Now→long millis→Logs, sorting, expiry
byte[] image

4. Images, files and network data

Raw data such as image pixels, PDF files and network packets is handled as byte arrays. A photo upload is millions of bytes, so the smallest type matters.

Photo upload→byte[] data→Saved to storage
अ ₹ A

5. Indian languages and symbols

Because char is Unicode, Java handles Devanagari, Tamil, Bengali and the ₹ sign naturally. That's why Java apps serve users in many Indian languages.

'₹' = 8377→char / String (UTF-16)→Shown correctly

Examples are simplified to show type choices. Each company's design varies.

7 Run the code

Riya's marks sheet, with the right types

public class Main {
    public static void main(String[] args) {
        byte age = 20;
        int rollNo = 2041;
        long aadhaarLikeId = 9_876_543_210L;
        double percentage = 87.5;
        char grade = 'A';
        boolean passed = percentage >= 40;

        System.out.println("Age: " + age + ", Roll: " + rollNo);
        System.out.println("ID: " + aadhaarLikeId);
        System.out.println("Percentage: " + percentage + ", Grade: " + grade);
        System.out.println("Passed: " + passed);
        System.out.println("int max: " + Integer.MAX_VALUE);
    }
}
Age: 20, Roll: 2041 ID: 9876543210 Percentage: 87.5, Grade: A Passed: true int max: 2147483647
  • byte age = 20;Ages fit easily in a byte (−128 to 127).
  • 9_876_543_210LBigger than int's maximum, so it needs long and the L suffix. Underscores are just for reading.
  • double percentage = 87.5;Decimal values default to double.
  • char grade = 'A';Single quotes for one character. Double quotes would make a String.
  • percentage >= 40A comparison: its result is a boolean, stored in passed.
8 Line by line

Six types in one frame, and an overflow

All these primitives live directly in main's stack frame: no arrows, no heap objects (except the strings we print).

Worked examples: real problems, solved and traced in memory

Choosing the right primitive type is a real design decision: too small and numbers overflow, decimals in double aren't exact, and char is secretly a number. Each program below shows one of these in action.

Example 1E-commerce · product

Pick a type for every field of a product

Problem statement

An online store keeps, for each product: number in stock, total units ever sold, price, rating, size letter and whether it's in stock. Pick the most suitable primitive type for each and print a product summary.

Input
stock 42, sold 3,200,000,000 (all time), price 1499.00, rating 4.3, size 'M', in stock
Expected output
Stock 42, sold 3200000000
Price 1499.0, rating 4.3, size M, available true

Approach

  1. int for small counts; long for counts beyond 2.1 billion.
  2. double for price and rating (decimals).
  3. char for one letter, boolean for yes/no.

Solution: run it live

What happens in memory, step by step

Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.

Example 2Banking · interest

Why money shouldn't be stored in double

Problem statement

A banking app adds 10 paise (0.10) to a balance three times and expects 0.30. With double it gets something slightly different, because binary fractions can't hold 0.1 exactly. Show the problem and the usual fix: count in paise with long.

Input
0.10 added three times
Expected output
double: 0.30000000000000004 (equals 0.3? false)
paise:  30 = Rs 0.30

Approach

  1. Add 0.10 to a double three times and print it.
  2. Do the same in paise with a long.
  3. Compare both to the expected value.

Solution: run it live

What happens in memory, step by step

Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.

Example 3Ticketing · seats

char is a number: the next seat letter

Problem statement

A theatre names rows A, B, C… A customer is in row 'C' and wants the row 2 behind. Since char stores a character code, you can add to it. Print the code and the new row.

Input
row 'C', move 2 rows back
Expected output
Row C has code 67
Two rows back: E

Approach

  1. Store the row as a char.
  2. Its numeric code: (int) row.
  3. Add 2 and cast back to char.

Solution: run it live

What happens in memory, step by step

Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.

9 Quiz and revision

Check yourself

Quick revision

Lesson 2.2 in one screen

  • 8 primitives: byte 8, short 16, int 32, long 64, float 32, double 64, char 16 (Unicode), boolean (true/false).
  • Defaults: int for whole numbers, double for decimals. L for long literals, f for float.
  • Integer overflow wraps silently: MAX_VALUE + 1 = MIN_VALUE.
  • int ÷ int drops the fraction; % gives the remainder.
  • float/double are approximate: 0.1 + 0.2 ≠ 0.3. Never use them for money.
  • char is a number: 'A' = 65. boolean is never a number.
  • Primitives hold values directly (stack for locals); they are not objects.

Interview and viva questions

Why does Java have primitive types if it's object-oriented?

For speed and memory. A primitive is a plain value with no object overhead, so arithmetic is fast and arrays of numbers are compact. Wrapper classes (Integer, Double…) exist when you need an object version (Module 10).

Why is char 2 bytes in Java but 1 byte in C?

Java uses Unicode from the start, so a char holds a 16-bit UTF-16 code unit, enough for most of the world's scripts. Characters outside that range (some emoji, rare scripts) take two chars, called a surrogate pair.

What is the size of a boolean?

The language specification doesn't fix it. HotSpot typically uses one byte for boolean fields and array elements, and an int-sized slot for local variables. Only true and false are ever visible to your code.

Why is 0.1 + 0.2 not equal to 0.3?

double stores numbers in binary. 0.1 and 0.2 are repeating fractions in binary, so each is rounded to the nearest representable value. Their sum is slightly off: 0.30000000000000004. Compare doubles with a tolerance, and use BigDecimal or integer paise for money.

How do you detect integer overflow?

Use a bigger type (long), or Math.addExact, Math.multiplyExact and similar methods, which throw ArithmeticException on overflow instead of wrapping silently.

Practice problems

Pick the right primitive type, and see each type's limits and quirks. Every output is fixed. The last three are business tasks.

P1Easy

All eight types

Declare one variable of each primitive type and print them on one line.

Input
(none)
Expected output
10 300 70000 9000000000 2.5 3.14159 J true
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        byte b = 10; short s = 300; int i = 70000; long l = 9000000000L;
        float f = 2.5f; double d = 3.14159; char c = 'J'; boolean ok = true;
        System.out.println(b + " " + s + " " + i + " " + l + " " + f + " " + d + " " + c + " " + ok);
    }
}
10 300 70000 9000000000 2.5 3.14159 J true

How it works: byte, short, int, long hold whole numbers of increasing size; float and double hold decimals; char one character; boolean true/false.

P2Easy

Type limits

Print the smallest and largest byte.

Input
(none)
Expected output
-128 to 127
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        System.out.println(Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
    }
}
-128 to 127

How it works: a byte is 8 bits, so it holds −128 to 127.

P3Easy

Character code

Print the numeric code of 'A' and of 'a'.

Input
'A', 'a'
Expected output
65 97
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        System.out.println((int) 'A' + " " + (int) 'a');
    }
}
65 97

How it works: lowercase letters come 32 codes after uppercase ones.

P4Medium

Integer division vs double division

Print 7 / 2 and 7 / 2.0.

Input
7, 2
Expected output
3
3.5
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        System.out.println(7 / 2);
        System.out.println(7 / 2.0);
    }
}
3 3.5

How it works: if both operands are ints, the result is an int (the fraction is dropped).

P5Medium

byte overflow

Store 127 in a byte, add 1 with ++, and print it.

Input
127
Expected output
-128
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        byte level = 127;
        level++;
        System.out.println(level);
    }
}
-128

How it works: the value wraps around from 127 to −128.

P6Business problem

Price in paise

Store a price of Rs 249.50 as paise in a long and print it back in rupees and paise.

Input
249.50
Expected output
Rs 249.50
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        long paise = 24950;
        System.out.println("Rs " + paise / 100 + "." + paise % 100);
    }
}
Rs 249.50

How it works: money in the smallest unit is exact; divide and take the remainder to display it.

P7Business problem

In-stock flag

A product has 0 units. Store inStock as a boolean and print it, then print it for 15 units.

Input
0, 15
Expected output
In stock: false
In stock: true
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        int units = 0;
        boolean inStock = units > 0;
        System.out.println("In stock: " + inStock);
        units = 15;
        inStock = units > 0;
        System.out.println("In stock: " + inStock);
    }
}
In stock: false In stock: true

How it works: a comparison produces a boolean you can store and print.

P8Business problem

Daily visitors over a year

A website gets 80 lakh visits a day. Print the yearly total using long.

Input
8000000 × 365
Expected output
Yearly visits: 2920000000
Show solution and explanation
public class Main {
    public static void main(String[] args) {
        long daily = 8_000_000;
        System.out.println("Yearly visits: " + daily * 365);
    }
}
Yearly visits: 2920000000

How it works: 2,920,000,000 is larger than an int can hold; long has room.

Real-world scenario questions (asked in company interviews)

Type choices look small, but they cause real money and data problems. Decide your answer first, then open the model answer.

Fintech · reconciliationFresher

The invoice total that's off by one paisa

An invoicing service stores prices as double. Finance reports that a few thousand invoices each month are off by ₹0.01 compared with the bank statement. Each line item looks correct. What's the likely cause, and how would you fix it?

The interviewer is checking: do you know that floating-point types are approximate, and the standard ways to handle money?

Show a strong answer

double stores values like 0.10 approximately in binary. When many items are added, multiplied by tax rates and rounded at different points, the tiny errors can push a total over a rounding boundary by one paisa.

  • Store amounts as whole paise in a long, or use BigDecimal with an explicit rounding mode (for example HALF_UP) and scale 2.
  • Decide and document where rounding happens (per line or per invoice), matching the finance/tax rules.
  • Store the same exact type in the database (a DECIMAL column, not FLOAT).
  • Add tests with known tricky values like 0.1 + 0.2.

Avoid: “Just round at the end with Math.round.” It hides some cases but doesn't make double exact.

E-commerce · analytics1–3 years

Negative revenue in the sales report

After a big festival sale, the daily dashboard shows negative total revenue. The code adds up order amounts in paise into an int total. On normal days the numbers are fine. What happened?

The interviewer is checking: do you recognise integer overflow, and do you know how to prevent it?

Show a strong answer

An int can hold at most 2,147,483,647, which in paise is only about ₹2.15 crore. The sale pushed the total past that, so the value wrapped around to a negative number without any error.

  • Use long for totals (up to about 9.2 × 1018 paise), or BigDecimal.
  • Use Math.addExact where overflow must never pass silently; it throws an exception instead.
  • Check the database column and any reporting code for the same limit.
  • Add a test with values near the limit, and alerting on impossible values like negative revenue.

Avoid saying: “Java would throw an error if the number was too big.” It doesn't for int and long arithmetic.

Social app · growth1–3 years

New users can't sign up

A fast-growing app suddenly fails to create new accounts. The logs show user IDs becoming negative. The database ID column is a 64-bit BIGINT, but the Java code reads IDs into an int userId. Why now, after years of working fine?

The interviewer is checking: do you understand type ranges across systems, and the need to keep Java and database types consistent?

Show a strong answer

The IDs only just crossed int's maximum of 2,147,483,647. Until then every ID fitted in an int. The next ID no longer fits; converting it to int keeps only the low 32 bits, which can produce a negative or wrong number.

  • Change the Java type to long everywhere the ID flows: entities, APIs, caches and clients.
  • Check other services and mobile apps that consume the ID, since they may have the same limit.
  • Rule of thumb: Java types should match the database column (BIGINT → long).

Avoid: “Reset the ID counter.” That risks duplicate IDs and doesn't fix the type mismatch.

IoT · cold-chain monitoringFresher

The temperature alert that never fires

A vaccine fridge monitor calculates an average temperature as a double and raises an alert with if (avgTemp == 8.0). Testers saw the reading pass through 8 degrees, but no alert came. Why?

The interviewer is checking: do you know not to compare floating-point values with ==?

Show a strong answer

A calculated double is rarely exactly 8.0. It might be 7.999999999 or 8.000000001, so the == check is almost never true.

  • For thresholds, use a range: if (avgTemp >= 8.0), which also catches values that jump past 8.
  • When equality is really needed, compare with a tolerance: Math.abs(a - b) < 0.0001.
  • Test with values just below, at and just above the limit.

Avoid: using == with float or double results of calculations.