Java Data Types & Type Casting
Explore all 8 primitive data types (byte, short, int, long, float, double, char, boolean), non-primitive types, literals, and implicit/explicit type casting.
- Widening (Implicit): Converting a smaller type to a larger type happens automatically with no data loss:
byte -> short -> int -> long -> float -> double(andchar -> int). - Narrowing (Explicit): Converting a larger type to a smaller type requires an explicit cast
(targetType)and may truncate decimals or overflow! - Numeric Promotion in Expressions: Arithmetic on
byte,short, orcharautomatically promotes operands toint. That is whybyte a = 10, b = 20; byte c = (byte)(a + b);requires a cast.
Introduction
Java is a statically typed language: every variable must declare its data type before use, and the compiler verifies type compatibility on every assignment and method call. Data types fall into two categories:
- 1Primitive Types (8 built-in): Store raw values directly in memory (zero object header overhead).
- 2Non-Primitive / Reference Types: Store references (memory addresses) to objects on the Heap (
String, arrays, classes, interfaces, enums, records).
The 8 Primitive Data Types
| Type | Size | Range / Precision | Default (Fields) | Suffix / Literal |
|---|---|---|---|---|
byte | 1 byte (8-bit) | -128 to 127 | 0 | byte b = 100; |
short | 2 bytes (16-bit) | -32,768 to 32,767 | 0 | short s = 30000; |
int | 4 bytes (32-bit) | -2,147,483,648 to 2,147,483,647 | 0 | int n = 1_000_000; |
long | 8 bytes (64-bit) | -2^63 to 2^63 - 1 | 0L | long pop = 8_100_000_000L; |
float | 4 bytes (32-bit IEEE 754) | ~6–7 decimal digits | 0.0f | float f = 9.81f; |
double | 8 bytes (64-bit IEEE 754) | ~15–16 decimal digits | 0.0d | double d = 3.14159; |
char | 2 bytes (UTF-16) | 0 to 65,535 | '\u0000' | char c = 'J'; |
boolean | 1 bit (JVM-dependent) | true or false | false | boolean ok = true; |
Type Promotion, Widening & Narrowing
- Widening (Implicit): Converting a smaller type to a larger type happens automatically with no data loss:
byte -> short -> int -> long -> float -> double(andchar -> int). - Narrowing (Explicit): Converting a larger type to a smaller type requires an explicit cast
(targetType)and may truncate decimals or overflow! - Numeric Promotion in Expressions: Arithmetic on
byte,short, orcharautomatically promotes operands toint. That is whybyte a = 10, b = 20; byte c = (byte)(a + b);requires a cast.
Real-World Application
Financial and e-commerce systems never use float or double for money because binary floating-point cannot represent 0.1 exactly (e.g., 0.1 + 0.2 == 0.30000000000000004). Instead, use java.math.BigDecimal or integer minor units (paise/cents).
Common Mistakes
- Forgetting the
Lsuffix onlongliterals larger thanInteger.MAX_VALUE. - Assuming
booleancan be cast toint(unlike C/C++, Java forbids(int) true).
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)