Java Data Types
Primitive Data Types
Java has 8 built-in primitive data types. They are not objects and hold their values directly in memory.
| Type | Size | Default | Range |
|---|---|---|---|
byte | 1 byte | 0 | -128 to 127 |
short | 2 bytes | 0 | -32,768 to 32,767 |
int | 4 bytes | 0 | -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | 0L | -9.2×10¹⸠to 9.2×10¹⸠|
float | 4 bytes | 0.0f | ~±3.4×10³â¸ (7 decimal digits) |
double | 8 bytes | 0.0d | ~±1.7×10³â°â¸ (15 decimal digits) |
char | 2 bytes | '\u0000' | 0 to 65,535 (Unicode) |
boolean | 1 bit | false | true or false |
public class PrimitiveTypes {
public static void main(String[] args) {
byte b = 100;
short s = 30000;
int i = 2_000_000; // underscores improve readability
long l = 9_000_000_000L;
float f = 3.14f;
double d = 3.141592653589793;
char c = 'A';
boolean flag = true;
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + flag);
}
}
Wrapper Classes
Every primitive type has a corresponding wrapper class in java.lang. Wrapper classes allow primitives to be used as objects (e.g., in collections).
| Primitive | Wrapper Class | Useful Methods |
|---|---|---|
| byte | Byte | Byte.parseByte(), Byte.MAX_VALUE |
| short | Short | Short.parseShort() |
| int | Integer | Integer.parseInt(), Integer.MAX_VALUE, Integer.toBinaryString() |
| long | Long | Long.parseLong() |
| float | Float | Float.parseFloat() |
| double | Double | Double.parseDouble() |
| char | Character | Character.isLetter(), Character.toUpperCase() |
| boolean | Boolean | Boolean.parseBoolean() |
Autoboxing and Unboxing
Autoboxing is the automatic conversion of a primitive to its wrapper class. Unboxing is the reverse — wrapper to primitive. Java does this automatically.
import java.util.ArrayList;
public class Autoboxing {
public static void main(String[] args) {
// Autoboxing: int -> Integer
int primitive = 42;
Integer wrapped = primitive; // compiler inserts Integer.valueOf(42)
// Unboxing: Integer -> int
int back = wrapped; // compiler inserts wrapped.intValue()
// Autoboxing in collections (collections only accept objects)
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // autoboxed to Integer.valueOf(10)
int val = list.get(0); // unboxed automatically
System.out.println("Wrapped: " + wrapped);
System.out.println("Back: " + back);
System.out.println("From list: " + val);
}
}
Type Casting
Widening casting (implicit) converts a smaller type to a larger type automatically. Narrowing casting (explicit) requires a cast operator and may lose data.
public class TypeCasting {
public static void main(String[] args) {
// Widening: byte -> short -> int -> long -> float -> double
int i = 100;
long l = i; // implicit widening
double d = l; // implicit widening
System.out.println("Widening: " + d); // 100.0
// Narrowing: double -> int (explicit cast required)
double pi = 3.99;
int truncated = (int) pi; // fractional part is dropped, NOT rounded
System.out.println("Narrowing: " + truncated); // 3
// char <-> int
char ch = 'A';
int ascii = ch; // widening: char to int
char back = (char) 66; // narrowing: int to char
System.out.println("char to int: " + ascii); // 65
System.out.println("int to char: " + back); // B
}
}
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.