1) Introduction
Java has 8 primitive data types. These are the most basic containers for values and are not objects. All other data structures (like String, arrays, and classes) build on top of these primitives.
Why it matters: choosing the right primitive affects memory, speed, and precision.
2) The 8 Primitive Data Types
A) Integer Types
- byte — 8 bits; range: −128 .. 127
- short — 16 bits; range: −32,768 .. 32,767
- int — 32 bits; range: −2,147,483,648 .. 2,147,483,647 (default for whole numbers)
- long — 64 bits; very large range (use
Lsuffix)
byte
byte age = 25;
Great for memory‑tight arrays or raw data (files, streams).
short
short year = 2025;
Use rarely; typically int is fine unless memory is critical.
int
int population = 330_000_000;
Default integer type. Readable underscores are allowed in numeric literals.
long
long distanceToMars = 225_000_000_000L;
Remember the L suffix to avoid int overflow.
B) Floating‑Point Types
- float — 32 bits; ~7 decimal digits; needs an
fsuffix - double — 64 bits; ~15 decimal digits; default for decimals
float
float pi = 3.1415927f;
Use when memory is tight or for large float arrays (graphics, sensors).
double
double gravity = 9.80665;
Default for scientific/financial approximations (but see precision note below).
C) Other Types
- char — 16‑bit Unicode character (single quotes)
- boolean — logical
true/false
char
char grade = 'A';
char symbol = '#';
char newline = '\n';
Stores a single UTF‑16 code unit; escape sequences like \n are allowed.
boolean
boolean isReady = true;
boolean doorOpen = false;
Drives control flow in if, while, and logical operations.
3) Default Values (for fields, not locals)
| Type | Default |
|---|---|
| byte, short, int, long | 0 |
| float, double | 0.0 |
| char | '\u0000' (null char) |
| boolean | false |
Important: Local variables have no default—Java requires you to initialize them before use.
4) Numeric Literals & Suffixes
- Underscores improve readability:
int n = 1_000_000; - Binary/hex supported:
int mask = 0b1010;,int color = 0xFFEECC; longneedsL(orl):long big = 3_000_000_000L;floatneedsf:float r = 2.5f;
5) Casting & Type Promotion
Widening (implicit)
int x = 10;
double y = x; // OK: int → double
Safe: smaller → larger container.
Narrowing (explicit)
double a = 9.8;
int b = (int) a; // 9 (truncates)
Risk of data loss: fractions chopped, ranges overflow.
In arithmetic, byte and short are promoted to int before operations. Mixing int and double yields double.
6) Overflow, Precision, and BigDecimal
- Overflow: integers wrap around silently.
int max = Integer.MAX_VALUE; int oops = max + 1; // wraps negative - Floating point:
float/doubleare approximate (binary IEEE‑754).double sum = 0.1 + 0.2; // 0.30000000000000004 - For money, prefer
java.math.BigDecimal.import java.math.BigDecimal; BigDecimal price = new BigDecimal("19.99");
7) Quick Reference Table
| Type | Bits | Typical Range / Precision | Example |
|---|---|---|---|
| byte | 8 | −128 .. 127 | byte b = 10; |
| short | 16 | −32,768 .. 32,767 | short s = 300; |
| int | 32 | ~ −2.1B .. +2.1B | int n = 5_000; |
| long | 64 | Very large | long id = 9_000_000_000L; |
| float | 32 | ~7 decimal digits | float f = 3.14f; |
| double | 64 | ~15 decimal digits | double d = 3.14159; |
| char | 16 | UTF‑16 code unit | char c = 'A'; |
| boolean | — | true / false | boolean ok = true; |
8) Ready‑to‑Use Snippets
Demo: declarations & casting
// Declarations
int count = 42;
long big = 3_000_000_000L;
double price = 19.99;
float ratio = 0.75f;
char initial = 'J';
boolean isAdmin = false;
// Widening: int → double
double cost = count;
// Narrowing: double → int (truncates)
int whole = (int) price;
// Promotion in expressions (byte/short → int, int + double → double)
short a = 5;
short b = 7;
int sum = a + b;
double avg = sum / 2.0;
Practice prompts
- Pick the smallest integer type that can store the value
50,000. Explain why. - Why is
doublepreferred overfloatfor most decimal work? - Show an example where integer overflow occurs. How could you prevent it?