Java Tutorial: Data Types & Casting
Java's 8 primitive types, the String class, type casting, autoboxing, and wrapper classes.
Strict types, zero surprises
Java has 8 primitive types and one very special class — `String`. Understanding each type's size and range prevents silent overflow bugs that can corrupt calculations.
The type system catches mistakes at compile time — before your bank transaction rounds wrong or your sensor reading overflows.
What you'll learn in this Java data types & casting tutorial
This interactive Java tutorial has 8 hands-on exercises. Estimated time: 20 minutes.
- The 8 primitive types — Java has exactly 8 primitive data types:
- int vs long — don't overflow — An `int` can hold up to about 2.1 billion. Go bigger and it overflows — silently wrapping to a negative number.
- double vs float — precision matters — `double` is the standard choice for decimals — 15-16 significant digits.
- char — single characters — `char` holds exactly one character using single quotes. Under the hood, it's a 16-bit Unicode code point.
- Type casting — **Widening** (safe — automatic): `int → long → double` — bigger type, no data loss.
- String — the special type — `String` starts with a capital S — it's a class, not a primitive. But it's used so often Java treats it almost like a pr…
- Wrapper classes and autoboxing — Each primitive has a **wrapper class**: `int → Integer`, `double → Double`, `boolean → Boolean`.
- Build a unit converter — No starter code. Build a temperature converter:
Java Data Types & Casting concepts covered
- Strict types, zero surprises