Java
Module 2: Java Fundamentals, Variables & Data TypesBeginner25 min read

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.

Key Takeaways & Core Concepts
  • Widening (Implicit): Converting a smaller type to a larger type happens automatically with no data loss: byte -> short -> int -> long -> float -> double (and char -> 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, or char automatically promotes operands to int. That is why byte 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:

  1. 1
    Primitive Types (8 built-in): Store raw values directly in memory (zero object header overhead).
  2. 2
    Non-Primitive / Reference Types: Store references (memory addresses) to objects on the Heap (String, arrays, classes, interfaces, enums, records).
Primitives, Widening & Narrowing Casting
1public class Main {
2 public static void main(String[] args) {
3 int score = 95;
4 double widened = score; // Implicit widening: int -> double
5
6 double average = 89.76;
7 int narrowed = (int) average; // Explicit narrowing: truncates .76
8
9 long worldPop = 8_100_000_000L; // 'L' suffix required
10 float gravity = 9.81f; // 'f' suffix required
11
12 System.out.println("Widened: " + widened);
13 System.out.println("Narrowed: " + narrowed);
14 System.out.println("World Pop: " + worldPop + ", Gravity: " + gravity);
15 }
16}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
16 lines
Note: Casting `double` to `int` truncates the fractional part toward zero rather than rounding.

The 8 Primitive Data Types

TypeSizeRange / PrecisionDefault (Fields)Suffix / Literal
byte1 byte (8-bit)-128 to 1270byte b = 100;
short2 bytes (16-bit)-32,768 to 32,7670short s = 30000;
int4 bytes (32-bit)-2,147,483,648 to 2,147,483,6470int n = 1_000_000;
long8 bytes (64-bit)-2^63 to 2^63 - 10Llong pop = 8_100_000_000L;
float4 bytes (32-bit IEEE 754)~6–7 decimal digits0.0ffloat f = 9.81f;
double8 bytes (64-bit IEEE 754)~15–16 decimal digits0.0ddouble d = 3.14159;
char2 bytes (UTF-16)0 to 65,535'\u0000'char c = 'J';
boolean1 bit (JVM-dependent)true or falsefalseboolean 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 (and char -> 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, or char automatically promotes operands to int. That is why byte 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 L suffix on long literals larger than Integer.MAX_VALUE.
  • Assuming boolean can be cast to int (unlike C/C++, Java forbids (int) true).
Practice & AssessmentJava Programming Tutorial

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)