← 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
| Type | Size | Description | Example |
|---|---|---|---|
| int | 4 bytes | Whole numbers | int age = 25; |
| double | 8 bytes | Decimal numbers | double price = 19.99; |
| boolean | 1 bit | true or false | boolean isActive = true; |
| char | 2 bytes | Single character | char grade = 'A'; |
| String | varies | Text (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);
}
}