Java Learner logo
← Back to Learn
Beginner

Basics & Foundations

Start your Java journey by learning the fundamental building blocks of the language.

Topics

Basics & Foundations

Variables & Data Types

Variables are containers for storing data values. In Java, every variable must have a data type.

Primitive Data Types

TypeSizeDescriptionExample
int4 bytesWhole numbersint age = 25;
double8 bytesDecimal numbersdouble price = 19.99;
boolean1 bittrue or falseboolean isActive = true;
char2 bytesSingle characterchar grade = 'A';
StringvariesText (not primitive)String name = "John";

Example

public class Variables {
    public static void main(String[] args) {
        // Integer
        int age = 25;
        
        // Double (decimal)
        double price = 19.99;
        
        // Boolean
        boolean isStudent = true;
        
        // Character
        char grade = 'A';
        
        // String
        String name = "Alice";
        
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Price: $" + price);
        System.out.println("Is Student: " + isStudent);
        System.out.println("Grade: " + grade);
    }
}