Data Types in Java
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
| Family | What the variable holds | Examples |
|---|---|---|
| Primitive | The value itself | int, double, boolean, char and four more |
| Reference (non-primitive) | A reference (an address) to an object on the heap | String, arrays, ArrayList, your own classes |
The Eight Primitive Types
| Type | Size | Range or values | Example |
|---|---|---|---|
byte | 8 bits | -128 to 127 | byte level = 3; |
short | 16 bits | -32,768 to 32,767 | short year = 2026; |
int | 32 bits | about -2.1 billion to 2.1 billion | int fare = 450; |
long | 64 bits | about ±9.2 × 10^18 | long population = 1_430_000_000L; |
float | 32 bits | about 7 significant digits | float rating = 4.5f; |
double | 64 bits | about 15 to 16 significant digits | double price = 249.99; |
char | 16 bits | one UTF-16 character, 0 to 65,535 | char grade = 'A'; |
boolean | not fixed by the JVM specification | true or false | boolean paid = true; |
Ask Java for the Limits
Every primitive type has a wrapper class (Integer, Long, Double and so on) that knows its limits:
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);
}
}byte: -128 to 127
short: -32768 to 32767
int: -2147483648 to 2147483647
long: -9223372036854775808 to 9223372036854775807
char: 0 to 65535How it works
- Lines 2 to 4 define a small helper method,
show, that prints one line. You'll write your own methods in "Methods in Java". - Each wrapper class has
MIN_VALUEandMAX_VALUEconstants, passed toshowon lines 7 to 11. showtakeslongvalues, so every smaller type, evenchar, is converted to a number automatically. That's widening, explained in "Type Casting and Type Conversion".- 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:
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]);
}
}Pune has 4 letters
First mark: 78Reference 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
- 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).
- Harder: Store India's population (about 1.43 billion) in a variable and multiply it by 2. Try it with
intfirst, thenlong. What changes?
Key Takeaways
Was this tutorial helpful?