Java Data Types: The Complete Guide

Understand what data types are and learn each type step by step. Master the building blocks of Java variables!

What are Data Types?

A data type is a label that tells Java what kind of information a variable will store. Just like a mailbox has different sizes for different packages, Java variables have different types for different kinds of data.

Without data types, Java wouldn't know:

  • If a number is a whole number (like 5) or a decimal (like 5.5)
  • If you're storing text (like "Hello") or a number
  • If something is true or false
  • How much memory to set aside for the variable

💡 Think of it this way: Data types are like containers. You have small boxes for small things and big boxes for big things. Putting a large item in a small box won't work, and putting a small item in a huge box wastes space. Java is the same way!

Two Main Categories of Data Types

1. Primitive Data Types

Basic, built-in data types that store single, simple values. Java provides 8 primitive types: integers, decimals, characters, and booleans.

2. Non-Primitive (Reference) Data Types

More complex types that can store multiple values and have special abilities. Examples: Strings, Arrays, Classes, Objects.

🎯 Key Difference: Primitive types store actual values (like 5 or true), while non-primitive types store references to objects.

Primitive Type #1: byte - Very Small Whole Numbers

What it does: Stores very small whole numbers (saves memory)

Range: -128 to 127


public class ByteExample {
    public static void main(String[] args) {
        byte age = 25;
        System.out.println("Age: " + age);
    }
}
Output:
Age: 25

Primitive Type #2: short - Small Whole Numbers

What it does: Stores small whole numbers

Range: -32,768 to 32,767


public class ShortExample {
    public static void main(String[] args) {
        short population = 5000;
        System.out.println("Population: " + population);
    }
}
Output:
Population: 5000

Primitive Type #3: int - Standard Whole Numbers ⭐

What it does: The most commonly used integer type for whole numbers

Range: -2,147,483,648 to 2,147,483,647

When to use: Use this for most whole numbers!


public class IntExample {
    public static void main(String[] args) {
        int studentCount = 500;
        int year = 2024;
        int temperature = -5;
        
        System.out.println("Students: " + studentCount);
        System.out.println("Year: " + year);
        System.out.println("Temp: " + temperature);
    }
}
Output:
Students: 500
Year: 2024
Temp: -5

Primitive Type #4: long - Very Large Whole Numbers

What it does: Stores very large whole numbers

Range: Huge numbers (up to 9 quintillion!)

Special Note: Add 'L' at the end: 123456789L


public class LongExample {
    public static void main(String[] args) {
        long worldPopulation = 8000000000L;
        long fileSize = 1073741824L;
        
        System.out.println("Population: " + worldPopulation);
        System.out.println("File Size: " + fileSize);
    }
}
Output:
Population: 8000000000
File Size: 1073741824

Primitive Type #5: float - Decimal Numbers

What it does: Stores decimal numbers (numbers with a point)

Precision: About 6-7 decimal digits

Special Note: Add 'f' at the end: 19.99f


public class FloatExample {
    public static void main(String[] args) {
        float price = 19.99f;
        float temperature = 36.5f;
        
        System.out.println("Price: $" + price);
        System.out.println("Temp: " + temperature + "°C");
    }
}
Output:
Price: $19.99
Temp: 36.5°C

Primitive Type #6: double - Decimal Numbers ⭐

What it does: Stores decimal numbers with high precision

Precision: About 15-17 decimal digits (very accurate!)

When to use: Use this for most decimal numbers!


public class DoubleExample {
    public static void main(String[] args) {
        double gpa = 3.85;
        double pi = 3.14159265359;
        double salary = 5000.50;
        
        System.out.println("GPA: " + gpa);
        System.out.println("Pi: " + pi);
        System.out.println("Salary: $" + salary);
    }
}
Output:
GPA: 3.85
Pi: 3.14159265359
Salary: $5000.5

Primitive Type #7: char - Single Character

What it does: Stores a single character (letter, digit, or symbol)

Special Note: Use single quotes: 'A' not "A"


public class CharExample {
    public static void main(String[] args) {
        char grade = 'A';
        char initial = 'J';
        char symbol = '@';
        
        System.out.println("Grade: " + grade);
        System.out.println("Initial: " + initial);
        System.out.println("Symbol: " + symbol);
    }
}
Output:
Grade: A
Initial: J
Symbol: @

Primitive Type #8: boolean - True or False

What it does: Stores only two possible values: true or false

Uses: Making decisions in your code (if statements)


public class BooleanExample {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean hasGraduated = false;
        boolean isJavaFun = true;
        
        System.out.println("Is Student: " + isStudent);
        System.out.println("Has Graduated: " + hasGraduated);
        System.out.println("Java is Fun: " + isJavaFun);
    }
}
Output:
Is Student: true
Has Graduated: false
Java is Fun: true

Non-Primitive Type #1: String - Text Data ⭐

What it does: Stores text (multiple characters together)

Special Note: Use double quotes: "Hello World"


public class StringExample {
    public static void main(String[] args) {
        String name = "John Smith";
        String email = "john@example.com";
        String message = "Welcome to Java!";
        
        System.out.println("Name: " + name);
        System.out.println("Email: " + email);
        System.out.println("Message: " + message);
    }
}
Output:
Name: John Smith
Email: john@example.com
Message: Welcome to Java!

Non-Primitive Type #2: Array - Multiple Values

What it does: Stores multiple values of the same type in one variable

Example: Instead of 5 variables for scores, use 1 array!


public class ArrayExample {
    public static void main(String[] args) {
        // Create array of 5 scores
        int[] scores = {85, 90, 78, 92, 88};
        
        System.out.println("Score 1: " + scores[0]);
        System.out.println("Score 2: " + scores[1]);
        System.out.println("All Scores:");
        for (int i = 0; i < scores.length; i++) {
            System.out.println(scores[i]);
        }
    }
}
Output:
Score 1: 85
Score 2: 90
All Scores:
85
90
78
92
88

Complete Example: All Data Types Together


public class AllDataTypes {
    public static void main(String[] args) {
        // Whole numbers
        int age = 20;
        long population = 8000000000L;
        
        // Decimal numbers
        double gpa = 3.85;
        float height = 5.9f;
        
        // Single character
        char grade = 'A';
        
        // Text
        String name = "Alice";
        
        // True or False
        boolean isActive = true;
        
        // Multiple values
        int[] scores = {95, 87, 92};
        
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("GPA: " + gpa);
        System.out.println("Grade: " + grade);
        System.out.println("Active: " + isActive);
        System.out.println("Height: " + height + " ft");
    }
}
Output:
Name: Alice
Age: 20
GPA: 3.85
Grade: A
Active: true
Height: 5.9 ft

Quick Reference: All Data Types

Type Size Purpose Example
byte 1 byte Small numbers 25
short 2 bytes Medium numbers 5000
int 4 bytes Regular numbers ⭐ 500
long 8 bytes Very large numbers 8000000000L
float 4 bytes Decimals 19.99f
double 8 bytes Decimals (precise) ⭐ 3.85
char 2 bytes Single char 'A'
boolean 1 bit True/False true
String Variable Text ⭐ "Hello"
Array Variable Multiple values {1,2,3}

How to Choose the Right Data Type

  • Whole numbers? Use int (or long for huge numbers)
  • Decimal numbers? Use double (it's more accurate)
  • Single character? Use char (like grades: A, B, C)
  • Text? Use String
  • True/False? Use boolean
  • Many values? Use Array

🎯 Golden Rule: Start with int and double. They work for most cases. Learn the others as you advance!

Key Takeaways

  • Java has 8 primitive data types for basic values
  • Java has non-primitive types for more complex data
  • int and double are the most commonly used
  • String is special - it's non-primitive but very common
  • Char uses single quotes: 'A', String uses double quotes: "ABC"
  • Long numbers need L at the end: 8000000000L
  • Float numbers need f at the end: 19.99f
  • Boolean only has two values: true or false

✅ Next Step: Now that you understand data types, practice creating variables of each type and see how they work in real programs!