3.1 Primitive Data Types, Literals & Default Values
Key Takeaways
Java defines exactly eight primitive types: boolean, byte (8-bit), short (16-bit), char (16-bit unsigned), int (32-bit), long (64-bit), float (32-bit IEEE 754), and double (64-bit IEEE 754)
Integer literals default to int (requiring L or l for values beyond int range), while floating-point literals default to double (requiring f or F when assigned to float)
Underscores in numeric literals are valid only between digits; placing them at the start, end, next to a decimal point, next to L/f/D suffixes, or next to 0x/0b prefixes fails compilation
Instance and static fields receive default values (0, 0.0, false,
'\u0000', or null), whereas reading an uninitialized local variable causes a definite-assignment compile errorA comma-separated declaration has one base type, as in
int a = 1, b = 2;. Separate statements on the same line may specify different types.
Why Primitive Types Matter in Java SE 8
Primitive variables hold primitive values. Reference variables hold references to objects or null. For code tracing, learn the eight primitive types, their value ranges, literal syntax rules, and initialization behavior.
Unlike C or C++, Java fixes the size and signedness of every numeric primitive across all operating systems and CPU architectures. An int is always 32 bits wide regardless of whether the JVM runs on 32-bit or 64-bit hardware.
The Eight Primitive Data Types
Java divides its eight primitives into three categories: one boolean type, five integral types (byte, short, char, int, long), and two floating-point types (float, double).
| Type | Category | Bit Width | Lowest Value (Finite for Floating Point) | Highest Value (Finite for Floating Point) | Field Default |
|---|---|---|---|---|---|
boolean | Logical | JVM-dependent | false (only true / false) | true | false |
byte | Signed integer | 8-bit | () | () | 0 |
short | Signed integer | 16-bit | () | () | 0 |
char | Unsigned UTF-16 | 16-bit | ('\u0000') | ('\uffff', ) | '\u0000' |
int | Signed integer | 32-bit | () | () | 0 |
long | Signed integer | 64-bit | () | () | 0L |
float | IEEE 754 single | 32-bit | Approx. | Approx. | 0.0f |
double | IEEE 754 double | 64-bit | Approx. | Approx. | 0.0 |
Float.MIN_VALUE and Double.MIN_VALUE name the smallest positive nonzero values, about and , respectively.
Important
char is the only unsigned numeric primitive in Java. It stores a 16-bit UTF-16 code unit from 0 to 65535. Assigning a negative literal such as char c = -1; fails to compile, whereas char c = 65; compiles and stores 'A'.
Notice also that boolean in Java is strictly isolated from numeric types. Unlike C, you cannot write boolean ok = 1;, if (count), or (int) true; A condition in an if, while, or basic for can have type boolean or Boolean. Java unboxes a Boolean, and unboxing null throws NullPointerException.
Numeric Literal Syntax & Number Bases
A literal is a fixed value written directly in source code. Java assigns a default type to every numeric literal before evaluating the surrounding assignment or expression:
- Integer literals default to
int: Writinglong big = 3000000000;fails to compile (integer number too large) because the compiler parses3000000000as anintbefore assigning it tolong. Adding theL(orl) suffix makes it alongliteral:long big = 3000000000L;. Prefer uppercaseLso it is never misread as the digit1. A hexadecimal, octal, or binaryintliteral can use all 32 bits, so0xffffffffdenotes-1. - Floating-point literals default to
double: Writingfloat price = 19.99;fails to compile (incompatible types: possible lossy conversion from double to float). You must appendforF(19.99f) or apply an explicit cast ((float) 19.99). Scientific notation such as1.5e3is also adoubleunless suffixed withf/F(1.5e3f). An optionaldorDsuffix explicitly marks adoubleliteral (19.99d).
Java SE 7 and Java SE 8 support four integer radix formats:
public class LiteralBases {
public static void main(String[] args) {
int dec = 42; // Decimal (base 10): digits 0-9
int oct = 052; // Octal (base 8, prefix 0): 5*8 + 2 = 42
int hex = 0x2A; // Hexadecimal (base 16, prefix 0x or 0X): 2*16 + 10 = 42
int bin = 0b101010; // Binary (base 2, prefix 0b or 0B): 32 + 8 + 2 = 42
System.out.println(dec == oct && hex == bin); // prints true
}
}
Watch for invalid digits in non-decimal literals: int badOctal = 08; does not compile because octal allows only digits 0 through 7, and int badBin = 0b102; fails because binary allows only 0 and 1. Floating-point literals can also be expressed in hexadecimal exponential form (0x1.0p3), though decimal and e notation dominate exam questions.
Underscore Rules in Numeric Literals
Since Java 7, underscores (_) may appear inside numeric literals to group digits visually, just like commas in prose. However, the compiler enforces strict placement rules—an underscore is legal only between two digits.
| Literal | Valid? | Reason |
|---|---|---|
1_000_000 | Valid | Underscores appear strictly between digits |
1__000 | Valid | Multiple consecutive underscores between digits are allowed |
0b1010_0101 | Valid | Underscore sits between binary digits |
0_52 | Valid | Leading 0 is itself an octal digit, so _ separates digits 0 and 5 |
_100 | Not a numeric literal | It is an identifier, so it refers to a name that must be declared before use |
100_ | Compile error | Underscore after the last digit |
3_.14 or 3._14 | Compile error | Underscore adjacent to a decimal point . |
100_L or 3.14_f | Compile error | Underscore adjacent to a type suffix (L, l, f, F, d, D) |
0x_10 or 0_x10 | Compile error | Underscore adjacent to or inside the 0x / 0b radix prefix |
Warning
Underscores are purely a compile-time source feature. Passing "1_000" to Integer.parseInt("1_000") throws a NumberFormatException at runtime.
Field Defaults vs. Local Definite Assignment
Java treats fields (instance variables and static class variables) completely differently from local variables (variables declared inside a method, constructor, or initializer block).
- Instance and static fields are automatically zero-initialized when an object or class is created: numeric primitives become
0or0.0,charbecomes'\u0000',booleanbecomesfalse, and reference fields becomenull. Array elements receive the exact same defaults. - Local variables are never given default values. Declaring
int count;inside a method is valid as long as you never readcount, but attempting to read or returncountbefore every reachable execution path assigns it triggers a definite-assignment compile error (variable count might not have been initialized).
public class DefaultCheck {
int instanceCount; // defaults to 0
static boolean ready; // defaults to false
char symbol; // defaults to '\u0000' (code point 0)
String label; // defaults to null
public void demo(boolean flag) {
int localA; // compiles while unused
int localB;
if (flag) {
localB = 10;
}
// System.out.println(localA); // COMPILE ERROR: not initialized
// System.out.println(localB); // COMPILE ERROR: might not have been initialized
}
}
Even when if (flag) is followed by an else branch that assigns localB = 20;, the compiler allows reading localB afterward because every path guarantees assignment. By contrast, two sequential if statements (if (flag) localB = 10; if (!flag) localB = 20;) fail definite assignment unless flag is a compile-time constant expression, because the compiler does not track complementary runtime conditions.
Declaring Multiple Variables in One Statement
You may declare and initialize multiple variables in a single statement using commas after one base type. Brackets after an individual name can make that variable an array, as in int count, data[];.
int a = 1, b = 2, c; // Valid: a, b, and c are all int
double x = 1.5, y = x; // Valid: declarations evaluate left to right
// int m = 1, int n = 2; // COMPILE ERROR: cannot repeat type after comma
// int p, String q; // COMPILE ERROR: cannot mix different types
Notice also that Java evaluates multi-variable initializers strictly from left to right, so y = x is valid while int x = y, y = 1; fails because y is not yet in scope when x is initialized.
Which set of variable declarations compiles cleanly inside a Java SE 8 method?
int a = 08; float b = 2.5f;
long a = 100_L; double b = 3.14;
int a = 0_52, b = 0b1010_0101; float c = 1.5e2F;
int a = 1, String b = "ok";
Consider a class with an instance field char ch; and an instance method with a parameter String[] args containing int x; if (args.length > 0) x = 5; System.out.println(ch + " " + x);. What happens when compiling and running this class?
Compilation fails because local variable x might not have been initialized
Compilation succeeds and prints a null character followed by a space and 0 when no arguments are passed
Compilation fails because instance field ch has no explicit value
Compilation succeeds and throws a NullPointerException when printing ch
Which range accurately describes the values a Java char variable can store without an explicit cast?
-128 to 127
-32,768 to 32,767
-65,536 to 65,535
0 to 65,535
Sections you finish are checked off in the contents.