Java Data Types
Java divides its types into two families:
- Primitive types — eight built-in types that hold a simple value directly (
int,double,boolean, ...). - Reference types — everything else:
String, arrays, and every class you or the library define. A reference variable holds a pointer to an object stored elsewhere in memory.
The eight primitive types
| Type | Size | Range / values | Default |
|---|---|---|---|
byte |
8 bit | -128 to 127 | 0 |
short |
16 bit | -32,768 to 32,767 | 0 |
int |
32 bit | about ±2.1 billion | 0 |
long |
64 bit | about ±9.2 quintillion | 0L |
float |
32 bit | ~6–7 significant decimal digits | 0.0f |
double |
64 bit | ~15–16 significant decimal digits | 0.0 |
boolean |
– | true / false |
false |
char |
16 bit | one UTF-16 character | '\u0000' |
In everyday code you mostly use int, long, double, boolean and char.
Whole numbers
int is the default choice. Use long (with an L suffix on big literals) when values may exceed two billion. Underscores can make long literals readable:
class Main {
public static void main(String[] args) {
byte level = 120;
short year = 2026;
int stars = 1_500_000;
long distanceKm = 9_460_730_472_580L; // one light year
System.out.println(level + " " + year + " " + stars);
System.out.println(distanceKm);
System.out.println("int max: " + Integer.MAX_VALUE);
System.out.println("long max: " + Long.MAX_VALUE);
}
}120 2026 1500000
9460730472580
int max: 2147483647
long max: 9223372036854775807
Note: If a calculation goes past the maximum, it silently overflows and wraps around:
Integer.MAX_VALUE + 1is-2147483648.
Decimal numbers
double is the default for fractions. A float literal needs an f suffix. You can also use scientific notation with e:
class Main {
public static void main(String[] args) {
double price = 19.99;
float ratio = 0.5f;
double avogadro = 6.022e23;
System.out.println(price + " " + ratio + " " + avogadro);
System.out.println(0.1 + 0.2);
}
}19.99 0.5 6.022E23
0.30000000000000004
The last line is not a bug in Java: binary floating-point can't represent 0.1 exactly. For money, store whole cents in a long or use java.math.BigDecimal.
Booleans and characters
class Main {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isFishTasty = false;
char letter = 'J';
char next = (char) (letter + 1);
char fromCode = '\u0041'; // Unicode escape for 'A'
System.out.println(isJavaFun + " " + isFishTasty);
System.out.println(letter + " -> " + next);
System.out.println((int) letter);
System.out.println(fromCode);
}
}true false
J -> K
74
A
A char is really a small number (its Unicode code), which is why letter + 1 works.
Reference types
String is the reference type you will use most. Unlike primitives, reference types start with a capital letter, can be null (pointing to no object), and come with methods:
class Main {
public static void main(String[] args) {
String greeting = "Hello";
int[] numbers = {4, 8, 15};
String nothing = null;
System.out.println(greeting.length());
System.out.println(greeting.toUpperCase());
System.out.println(numbers.length);
System.out.println(nothing == null);
}
}5
HELLO
3
true
| Primitive | Reference | |
|---|---|---|
| Holds | the value itself | a reference to an object |
Can be null |
no | yes |
| Has methods | no | yes |
| Naming | lowercase (int) |
capitalised (String) |
Test yourself
Which type should you use for the number 5,000,000,000?
- int
- short
- long
- byte
Which of these is NOT a primitive type?
- char
- boolean
- String
- double
Why does 0.1 + 0.2 print 0.30000000000000004?
- A Java bug
- Binary floating point can't represent 0.1 exactly
- Because of integer overflow
- println rounds incorrectly