Data Types in Java

Naveen AnamaneniUpdated · 6 min read

A kitchen has different containers for different things: a small jar for salt, a big tin for rice, a bottle for oil. You wouldn't pour oil into the salt jar. Java's data types are its containers. Every variable has a type that says what kind of value it holds and how much memory that takes, and the compiler checks that you never pour the wrong thing in.

Two Families of Types

Primitive types hold values; reference types point to objects
FamilyWhat the variable holdsExamples
PrimitiveThe value itselfint, double, boolean, char and four more
Reference (non-primitive)A reference (an address) to an object on the heapString, arrays, ArrayList, your own classes

The Eight Primitive Types

TypeSizeRange or valuesExample
byte8 bits-128 to 127byte level = 3;
short16 bits-32,768 to 32,767short year = 2026;
int32 bitsabout -2.1 billion to 2.1 billionint fare = 450;
long64 bitsabout ±9.2 × 10^18long population = 1_430_000_000L;
float32 bitsabout 7 significant digitsfloat rating = 4.5f;
double64 bitsabout 15 to 16 significant digitsdouble price = 249.99;
char16 bitsone UTF-16 character, 0 to 65,535char grade = 'A';
booleannot fixed by the JVM specificationtrue or falseboolean paid = true;

Ask Java for the Limits

Every primitive type has a wrapper class (Integer, Long, Double and so on) that knows its limits:

Limits.java▶ Try it
public class Limits {
    static void show(String type, long min, long max) {
        System.out.println(type + min + " to " + max);
    }
 
    public static void main(String[] args) {
        show("byte:  ", Byte.MIN_VALUE, Byte.MAX_VALUE);
        show("short: ", Short.MIN_VALUE, Short.MAX_VALUE);
        show("int:   ", Integer.MIN_VALUE, Integer.MAX_VALUE);
        show("long:  ", Long.MIN_VALUE, Long.MAX_VALUE);
        show("char:  ", Character.MIN_VALUE, Character.MAX_VALUE);
    }
}
Output
byte:  -128 to 127
short: -32768 to 32767
int:   -2147483648 to 2147483647
long:  -9223372036854775808 to 9223372036854775807
char:  0 to 65535

How it works

  1. Lines 2 to 4 define a small helper method, show, that prints one line. You'll write your own methods in "Methods in Java".
  2. Each wrapper class has MIN_VALUE and MAX_VALUE constants, passed to show on lines 7 to 11.
  3. show takes long values, so every smaller type, even char, is converted to a number automatically. That's widening, explained in "Type Casting and Type Conversion".
  4. These limits are the same on every computer, which is part of Java's platform independence.

Try it yourself: add System.out.println(Double.MAX_VALUE); to main. How is such a large number printed?

Reference Types

Anything that isn't one of the eight primitives is a reference type. The variable holds a reference, and the object lives on the heap:

References.java▶ Try it
public class References {
    public static void main(String[] args) {
        String city = "Pune";
        int[] marks = {78, 92, 86};
        System.out.println(city + " has " + city.length() + " letters");
        System.out.println("First mark: " + marks[0]);
    }
}
Output
Pune has 4 letters
First mark: 78

Reference types can do more than hold a value: city.length() asks the String object a question. Primitives can't do that; they're just values. A reference variable can also hold null, meaning "no object"; a primitive can't.

Common Mistakes

Using int for big numbers. India's population, a phone number or milliseconds since 1970 don't fit in an int. Use long.

Thinking String is a primitive. It's a class, so it's a reference type. That's why it starts with a capital S.

Using char for text. A char holds a single character in single quotes: 'A'. Text uses String and double quotes: "A".

Interview Corner

What are the primitive data types in Java?

Eight: byte, short, int and long for whole numbers (8, 16, 32 and 64 bits), float and double for decimals (32 and 64 bits), char for a 16-bit UTF-16 character, and boolean for true or false.

What is the difference between primitive and reference types?

A primitive variable holds its value directly. A reference variable holds a reference to an object on the heap. References can be null and the objects have methods; primitives can't be null and have no methods. Every primitive has a matching wrapper class, such as Integer for int.

What is the size of boolean in Java?

The JVM specification doesn't fix it. Logically it holds one bit of information, but a JVM usually uses at least a byte, and in boolean arrays HotSpot uses one byte per element.

Practice

  1. Easy: Pick the best type for each: number of passengers on a train, distance between two cities in km with decimals, whether a ticket is canceled, the class letter of a coach (A, B or S).
  2. Harder: Store India's population (about 1.43 billion) in a variable and multiply it by 2. Try it with int first, then long. What changes?

Quick check: Data types

4 questions
1How many primitive types does Java have?
2Which type should hold a phone number like 9876543210 as a number?
3What does a String variable hold?
4Which can be null?

Key Takeaways

Was this tutorial helpful?

Lesson 34 of 482