Java is a strongly-typed programming language. This means every variable, expression, and literal must have an explicitly defined type known at compile time. Data types determine the size and layout of the variable's memory, the range of values that can be stored, and the operations that can be performed.
Figure 3: Complete Java 8 Primitive Data Types Taxonomy: Bits, Bytes, Numerical Limits, and Default Values.
Java data types are split into two broad categories:
byte (8 bits / 1 byte): Stores integers from -128 to +127. Useful for saving memory in massive binary data streams and image buffers.short (16 bits / 2 bytes): Stores integers from -32,768 to +32,767.int (32 bits / 4 bytes): Stores integers from -2,147,483,648 to +2,147,483,647 (~2 Billion). This is the default integral type in Java.long (64 bits / 8 bytes): Stores massive integers from -9 × 1018 to +9 × 1018. Suffix literals with an uppercase L (e.g. long distance = 9876543210L;).float (32 bits / 4 bytes): Single precision IEEE 754 floating point. Precision: ~6-7 decimal places. Must be suffixed with f or F (e.g. float pi = 3.14159f;).double (64 bits / 8 bytes): Double precision IEEE 754 floating point. Precision: ~15-16 decimal places. Default type for all decimal literals (e.g. double g = 9.80665;).char (16 bits / 2 bytes): Stores a single 16-bit Unicode character enclosed in single quotes ('A', '₹', 'A'). Range: 0 to 65,535.boolean (JVM-dependent, 1 bit logical): Stores either true or false. Used for all logical conditions and control-flow branches.Modern Java permits underscores inside numeric literals to enhance readability without impacting execution:
// Using underscores for readability (Java 7+)
long nationalDebt = 31_000_000_000_000L;
int creditCardNumber = 4111_2222_3333_4444;
double hexNumber = 0x1.0p0; // Hexadecimal floating point
// Binary, Octal, and Hexadecimal literals
int binaryVal = 0b1010_1010; // 170 in decimal
int octalVal = 077; // 63 in decimal (starts with 0)
int hexVal = 0xFF; // 255 in decimal (starts with 0x)
Exam Question: "Why does float f = 3.14; produce a compilation error in Java?"
Answer: In Java, all fractional literals (like 3.14) are treated by default as 64-bit double. Assigning a 64-bit double to a 32-bit float constitutes an unsafe narrowing conversion that could cause precision loss. To compile, you must either append the f literal suffix (float f = 3.14f;) or explicitly cast it (float f = (float) 3.14;).
Now that you understand data type limits, let us explore type conversion and casting rules. Proceed to: Java Type Casting & Conversions →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Widening vs Narrowing conversions.
Autoboxing and Object wrappers.
Reference type strings and SCP.
Arithmetic, relational, and bitwise.