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 error

  • A comma-separated declaration has one base type, as in int a = 1, b = 2;. Separate statements on the same line may specify different types.

Last updated: November 2026

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).

TypeCategoryBit WidthLowest Value (Finite for Floating Point)Highest Value (Finite for Floating Point)Field Default
booleanLogicalJVM-dependentfalse (only true / false)truefalse
byteSigned integer8-bit−128-128 (−27-2^7)127127 (27−12^7 - 1)0
shortSigned integer16-bit−32,768-32,768 (−215-2^{15})32,76732,767 (215−12^{15} - 1)0
charUnsigned UTF-1616-bit00 ('\u0000')65,53565,535 ('\uffff', 216−12^{16} - 1)'\u0000'
intSigned integer32-bit−2,147,483,648-2,147,483,648 (−231-2^{31})2,147,483,6472,147,483,647 (231−12^{31} - 1)0
longSigned integer64-bit−9,223,372,036,854,775,808-9,223,372,036,854,775,808 (−263-2^{63})9,223,372,036,854,775,8079,223,372,036,854,775,807 (263−12^{63} - 1)0L
floatIEEE 754 single32-bitApprox. −3.4028235×1038-3.4028235 \times 10^{38}Approx. 3.4028235×10383.4028235 \times 10^{38}0.0f
doubleIEEE 754 double64-bitApprox. −1.7976931348623157×10308-1.7976931348623157 \times 10^{308}Approx. 1.7976931348623157×103081.7976931348623157 \times 10^{308}0.0

Float.MIN_VALUE and Double.MIN_VALUE name the smallest positive nonzero values, about 1.4×10−451.4 \times 10^{-45} and 4.9×10−3244.9 \times 10^{-324}, 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: Writing long big = 3000000000; fails to compile (integer number too large) because the compiler parses 3000000000 as an int before assigning it to long. Adding the L (or l) suffix makes it a long literal: long big = 3000000000L;. Prefer uppercase L so it is never misread as the digit 1. A hexadecimal, octal, or binary int literal can use all 32 bits, so 0xffffffff denotes -1.
  • Floating-point literals default to double: Writing float price = 19.99; fails to compile (incompatible types: possible lossy conversion from double to float). You must append f or F (19.99f) or apply an explicit cast ((float) 19.99). Scientific notation such as 1.5e3 is also a double unless suffixed with f/F (1.5e3f). An optional d or D suffix explicitly marks a double literal (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.

LiteralValid?Reason
1_000_000ValidUnderscores appear strictly between digits
1__000ValidMultiple consecutive underscores between digits are allowed
0b1010_0101ValidUnderscore sits between binary digits
0_52ValidLeading 0 is itself an octal digit, so _ separates digits 0 and 5
_100Not a numeric literalIt is an identifier, so it refers to a name that must be declared before use
100_Compile errorUnderscore after the last digit
3_.14 or 3._14Compile errorUnderscore adjacent to a decimal point .
100_L or 3.14_fCompile errorUnderscore adjacent to a type suffix (L, l, f, F, d, D)
0x_10 or 0_x10Compile errorUnderscore 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 0 or 0.0, char becomes '\u0000', boolean becomes false, and reference fields become null. 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 read count, but attempting to read or return count before 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.

Test Your Knowledge

Which set of variable declarations compiles cleanly inside a Java SE 8 method?

A

int a = 08; float b = 2.5f;

B

long a = 100_L; double b = 3.14;

C

int a = 0_52, b = 0b1010_0101; float c = 1.5e2F;

D

int a = 1, String b = "ok";

Test Your Knowledge

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?

A

Compilation fails because local variable x might not have been initialized

B

Compilation succeeds and prints a null character followed by a space and 0 when no arguments are passed

C

Compilation fails because instance field ch has no explicit value

D

Compilation succeeds and throws a NullPointerException when printing ch

Test Your Knowledge

Which range accurately describes the values a Java char variable can store without an explicit cast?

A

-128 to 127

B

-32,768 to 32,767

C

-65,536 to 65,535

D

0 to 65,535

Sections you finish are checked off in the contents.