← Chapter 2 — Core Syntax & Data

7. Primitive Data Types

mixed

Description

Primitive types store simple values directly. Java provides integer, decimal, character, boolean, and byte-sized numeric primitives.

Choose a type that expresses the value clearly. An exam score is usually int, a price often uses BigDecimal in real financial programs, and a yes-or-no answer uses boolean.

Subtopics

Numbers

Description

byte, short, int, long, float, and double store numbers.

Explanation

int is a common whole-number choice; double is common for measured values.

Example

int score = 95;
double average = 87.5;

char and boolean

Description

char holds one character and boolean holds true or false.

Explanation

A char uses single quotes; a String uses double quotes.

Example

char grade = 'A';
boolean passed = true;

Explanation

Each primitive type has a limited range. Selecting long for very large whole numbers and double for fractional values avoids overflow or unwanted truncation.

Example

public class PrimitiveDemo {
    public static void main(String[] args) {
        int score = 95;
        char grade = 'A';
        boolean passed = score >= 40;
        System.out.println(grade + ": " + passed);
    }
}

Key points

  • Primitives store simple values.
  • char uses single quotes.
  • boolean is true or false.

Open reference