Learn Java
import com.learnjava.variablesdatatypes.PrimitiveTypes;

Primitive Data Types in Java

@Concept

Java has exactly eight primitive types, built directly into the language rather than being objects: byte, short, int, and long for whole numbers of increasing size; float and double for decimals; char for a single character; and boolean for true or false. Every primitive has a fixed bit width on every platform — int is always 32 bits, unlike C where int's size can vary by platform.

Primitives are stored directly rather than as a reference to an object elsewhere in memory, get a default value when used as a field (0, 0.0, false, and a null character for char), and each numeric type has a documented minimum and maximum. Going outside that range causes silent overflow rather than an error, which is a genuine practical gotcha worth knowing about early.

@HowItWorks
1

Integer family

byte (8-bit), short (16-bit), int (32-bit, the usual default), and long (64-bit, needs an L suffix on its literals).

2

Floating-point family

float (32-bit, needs an f suffix) and double (64-bit, the default type for any decimal literal).

3

Character type

char holds exactly one 16-bit Unicode character, written in single quotes.

4

Boolean type

Exactly two values, true and false, with no numeric stand-in — unlike C, 0 and 1 are not booleans in Java.

5

Fixed sizes everywhere

Every primitive's bit width is guaranteed identical on every platform, which is part of what keeps write-once-run-anywhere true at the data level too.

6

Overflow, not an error

Exceeding a type's range wraps silently around to the opposite end instead of throwing an exception, which is a common source of subtle bugs.

@Example
public class Primitives {
public static void main(String[] args) {
byte small = 127;
int normal = 2147483647;
int overflowed = normal + 1;
System.out.println("byte: " + small);
System.out.println("max int + 1 = " + overflowed);
}
}
Output
byte: 127
max int + 1 = -2147483648
1small holds 127, the maximum value a byte can represent.
2normal holds Integer.MAX_VALUE, the largest possible int.
3Adding 1 pushes past that limit, and Java wraps around to the most negative int instead of raising an error.
4Both println calls run once each, and the second line visibly shows the overflow happening.
@UseCases
Choosing long over int for values that could exceed roughly 2.1 billion, like millisecond timestamps
Debugging a silently wrong calculation caused by unnoticed integer overflow
Defaulting to double for decimals unless memory or precision specifically calls for float
Knowing why a boolean flag can't be swapped for 0 or 1 the way it can in C

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.