LearnFrenzy
Sign in Take a free test

Variables and Data Types · Lesson 5 of 42

Data Types: Primitives, Wrappers and String

5 min read Updated Nov 7, 2026 Saurabh Samir

Java has two families of types. Primitive types hold simple values directly – numbers, characters, true/false. Reference types – classes such as String, arrays and your own objects – hold a reference (an address) to an object elsewhere in memory. Knowing which is which explains a surprising number of interview questions.

In this lesson

  • The eight primitive types, their sizes and ranges
  • Integer overflow and floating-point precision
  • Primitives vs references in memory
  • Wrapper classes, autoboxing and the Integer cache trap

The eight primitives

TypeSizeRange / valuesExample
byte8 bits−128 to 127byte b = 100;
short16 bits−32,768 to 32,767short s = 3000;
int32 bitsabout ±2.1 billionint marks = 92;
long64 bitsabout ±9.2 × 10¹⁸long views = 9_000_000_000L;
float32 bits~7 decimal digitsfloat f = 3.14f;
double64 bits~15 decimal digitsdouble pi = 3.14159;
char16 bitsone Unicode characterchar grade = 'A';
boolean–true / falseboolean ok = true;

Defaults to remember: use int for whole numbers, long when values can exceed 2 billion (IDs, timestamps, bank balances in paise), and double for decimals. Note the L on long literals and the f on float literals.

public class Main {
    public static void main(String[] args) {
        System.out.println("int:  " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("long: " + Long.MAX_VALUE);
        System.out.println("char: " + (int) Character.MAX_VALUE);
        System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
    }
}

Output

int:  -2147483648 to 2147483647
long: 9223372036854775807
char: 65535
byte: -128 to 127

Overflow: when numbers wrap around

Integer types don't raise an error when they overflow – they silently wrap to the other end of the range. This has caused real production bugs.

public class Main {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        System.out.println(max + 1);                 // wraps around!
        long safe = (long) max + 1;
        System.out.println(safe);
        int secondsInYear = 365 * 24 * 60 * 60;
        System.out.println(secondsInYear * 100);     // overflow again
        System.out.println(secondsInYear * 100L);    // long arithmetic
    }
}

Output

-2147483648
2147483648
-1141367296
3153600000

Tip

Use Math.addExact(a, b) or Math.multiplyExact(a, b) when overflow must never happen – they throw ArithmeticException instead of wrapping. For money, use long paise or BigDecimal, never double.

Floating-point precision

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        System.out.println(0.1 + 0.2);
        System.out.println(0.1 + 0.2 == 0.3);
        System.out.println(new BigDecimal("0.1").add(new BigDecimal("0.2")));
        System.out.println(10 / 4);      // int division
        System.out.println(10 / 4.0);    // double division
    }
}

Output

0.30000000000000004
false
0.3
2
2.5

char is a number too

A char stores a 16-bit Unicode value, so you can do arithmetic with it:

public class Main {
    public static void main(String[] args) {
        char c = 'A';
        System.out.println((int) c);        // its code
        System.out.println((char) (c + 2)); // C
        System.out.println(c + 1);          // char + int → int
        char rupee = '₹';
        System.out.println(rupee);
    }
}

Output

65
C
66
₹

Primitives vs references

Where the value lives

Variable

  • Primitive: int marks = 92the value 92 is stored right in the variable (on the stack for locals)
  • Reference: String name = "Asha"the variable holds an address; the String object lives on the heap

Two practical consequences: == on references compares addresses, not contents (use .equals() for strings), and a reference can be null – pointing at nothing – while a primitive can't.

Wrapper classes and autoboxing

Each primitive has an object version: Integer, Long, Double, Character, Boolean and so on. Collections such as ArrayList can only hold objects, so Java converts automatically – autoboxing (int → Integer) and unboxing (Integer → int).

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> marks = new ArrayList<>();
        marks.add(85);                          // autoboxing
        int first = marks.get(0);               // unboxing
        System.out.println(first + 5);
        System.out.println(Integer.parseInt("120") + 1);
        System.out.println(Integer.toBinaryString(10));
        System.out.println(Character.isDigit('7') + " " + Character.toUpperCase('j'));
    }
}

Output

90
121
1010
true J

The Integer cache trap

public class Main {
    public static void main(String[] args) {
        Integer a = 127, b = 127;
        Integer c = 128, d = 128;
        System.out.println(a == b);       // same cached object
        System.out.println(c == d);       // two different objects
        System.out.println(c.equals(d));  // compare values
    }
}

Output

true
false
true

Common mistake: comparing wrappers with ==

Java caches Integer objects from −128 to 127, so == happens to work for small numbers and fails for larger ones. Always compare wrapper objects with .equals(). Also beware: unboxing a null Integer throws NullPointerException.

String in brief

String is a class, not a primitive, but Java gives it special treatment: literals in double quotes, + for joining, and a pool of reused literals. Strings are immutable – every "change" creates a new string. Chapter 6 covers strings in depth.

Practice

Try it yourself

  1. Calculate the number of milliseconds in 30 days without overflow.

    Show solution
    public class Main {
        public static void main(String[] args) {
            long ms = 30L * 24 * 60 * 60 * 1000;
            System.out.println(ms);
        }
    }

    Output

    2592000000
  2. Print the next three letters after 'x' using char arithmetic.

    Show solution
    public class Main {
        public static void main(String[] args) {
            char c = 'x';
            for (int i = 1; i <= 3; i++) {
                System.out.print((char) (c + i) + " ");
            }
        }
    }

    Output

    y z {

    Notice the third character is { – the next Unicode value after z, not a. Wrapping around the alphabet needs %.

Quick check

How many primitive types does Java have?

Eight: byte, short, int, long, float, double, char, boolean.

What is Integer.MAX_VALUE + 1?

Integer.MIN_VALUE (−2147483648) – integer overflow wraps around.

Why can't you write List<int>?

Generics work with objects only; use the wrapper type List<Integer>.

Summary

  • 8 primitives store values directly; everything else is a reference to an object.
  • Integers overflow silently; floats are approximate – use long/BigDecimal for money.
  • Wrappers allow primitives in collections; compare them with .equals(), not ==.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links