Curriculum / Java / Lesson 03

Data Types in Java

Java defines exactly 8 primitive types with fixed sizes that never change across platforms — this consistency is part of what makes Java code portable..

3 of 11
beginner 📖 5 min read 🎯 1 question

Data Types

Java defines exactly 8 primitive types with fixed sizes that never change across platforms — this consistency is part of what makes Java code portable.

💡 Key insight Unlike C, Java's int is always exactly 4 bytes on every platform — no guessing or sizeof() needed.

Syntax

syntax
int wholeNumber;
double decimalNumber;
char singleCharacter;
boolean trueOrFalse;

Example

Java's boolean is a true independent type — it can never be treated as 0 or 1 like in C.

java
public class Main {
    public static void main(String[] args) {
        int age = 20;
        double gpa = 8.9;
        char grade = 'A';
        boolean passed = true;
        System.out.println(age + " " + gpa + " " + grade + " " + passed);
    }
}
output20 8.9 A true

Try it yourself

Copy this example into the Java compiler and run it — try changing a value and see what happens.

Quick Quiz

1. How many primitive data types does Java define?

Back to curriculum