Data Types
Overview
Data Types define the exact size and format of data stored in a variable, allowing the JVM to optimize memory usage perfectly. Java splits its data types into two massive categories: Primitives and Reference Types.
Primitive Types are the raw, foundational building blocks. They hold simple values directly in Stack memory and have virtually no performance overhead. There are exactly 8 primitive types in Java:
- Integers: byte (1 byte), short (2 bytes), int (4 bytes, the default), long (8 bytes, for massive numbers).
- Decimals: float (4 bytes, less precise), double (8 bytes, the default for decimals).
- Characters: char (2 bytes, holds a single Unicode character).
- Booleans: boolean (1 bit, holds true/false).
Reference Types are complex objects (like String, Scanner, or your own custom classes). Instead of holding the actual data, a reference variable holds the memory address pointing to the massive object stored in the Heap memory.
Syntax
Notice the mandatory 'L' for long and 'f' for float. If you forget the 'f', Java assumes it is a double and throws a compilation error. Explicit casting requires putting the target type in parentheses.
public class Main {
public static void main(String[] args) {
// --- 8 Primitive Types ---
byte smallNum = 127; // Max size is 127
short mediumNum = 32000; // Max size is ~32,000
int defaultNum = 2000000000; // The default integer choice
long hugeNum = 9000000000000L; // Suffix 'L' required for long
float price = 19.99f; // Suffix 'f' required for float
double precisePrice = 19.9999; // The default decimal choice
char letter = 'A'; // Uses SINGLE quotes
boolean isJavaFun = true; // true or false
// --- Type Casting ---
// Implicit Casting (Automatic, smaller to larger)
int myInt = 9;
double myDouble = myInt; // Becomes 9.0 automatically
// Explicit Casting (Manual, larger to smaller, risks data loss!)
double preciseNum = 9.78;
int roundedNum = (int) preciseNum; // Becomes 9 (decimal is chopped off!)
}
}Common Pitfalls
- Integer Overflow. If you add 1 to the absolute maximum value an
intcan hold (2.14 billion), it wraps around to negative 2.14 billion! If you are dealing with global populations or massive finances, always uselong. - Forgetting the 'f' suffix on a float.
float x = 3.14;will fail to compile because 3.14 is considered adoubleby default, which is too large to fit in afloat. - Using double/float for exact currency calculations. Floats and doubles suffer from floating-point arithmetic precision errors (e.g.,
0.1 + 0.2might equal0.300000000004). For real banking apps, useBigDecimalinstead.
Interview Questions
Primitives (like int, double) hold raw values directly in the stack and are incredibly fast. Wrapper classes (like Integer, Double) are full Objects that wrap the primitive data, providing utility methods (like Integer.parseInt()). Wrapper classes are required when using generic collections like ArrayList.
double to an int?Because a double takes up 8 bytes of memory and holds decimal places, while an int only takes 4 bytes and holds whole numbers. Java forces you to explicitly cast it using (int) to acknowledge that you are actively choosing to lose the decimal data (truncation).
Real-World Example
NASA's Mars Rover software must carefully manage memory. Instead of using massive 8-byte long variables for simple sensor flags (like 0 to 100), they might explicitly use 1-byte byte types to preserve ultra-limited onboard RAM.
// Optimizing memory footprint
byte currentGear = 2; // Only uses 1 byte
long distanceToEarth = 12000000000L; // Requires 8 bytesCheck Your Knowledge
Test your understanding of Data Types with these quick questions.