Variables & The 8 Primitive Data Types
🟢 Beginner Level | Java Core Syntax
📖 Definition
A Variable is a named location in memory used to store data values. Java is a statically typed language featuring 8 Primitive Data Types: byte, short, int, long, float, double, boolean, and char.
🌐 Multilingual Explanations
English
Primitives store raw values directly on the JVM Stack memory. Each primitive has a fixed bit size (e.g. int is 32-bit, long is 64-bit).
Hindi
Java mein har variable ka data type declare karna zaroori hota hai. Primitive types direct values store karte hain Stack memory mein.
Marathi
Java madhye pratyek variable cha type sangava lagto. Primitive types raw values Stack memory madhye jatan kartat.
Hinglish
Variables are like labeled storage boxes. `int` for whole numbers, `double` for decimals, `boolean` for true/false.
📊 8 Primitive Data Types Reference Table
| Data Type | Size (Bits/Bytes) | Range / Values | Default Value |
|---|---|---|---|
byte | 8 bits (1 byte) | -128 to 127 | 0 |
short | 16 bits (2 bytes) | -32,768 to 32,767 | 0 |
int | 32 bits (4 bytes) | -231 to 231-1 | 0 |
long | 64 bits (8 bytes) | -263 to 263-1 (Requires L suffix) | 0L |
float | 32 bits (4 bytes) | Single precision floating point (Requires f suffix) | 0.0f |
double | 64 bits (8 bytes) | Double precision floating point | 0.0d |
boolean | 1 bit (JVM dependent) | true or false | false |
char | 16 bits (2 bytes) | Unicode characters (0 to 65,535) | ' ' |
⚡ Code Example
public class PrimitivesDemo {
public static void main(String[] args) {
int age = 25;
long population = 8000000000L; // 'L' suffix required for long
float pi = 3.14159f; // 'f' suffix required for float
double distance = 149600000.50;
boolean isActive = true;
char grade = 'A';
System.out.println("Age: " + age + ", Population: " + population);
}
}
⚠️ Common Pitfalls
Missing Literal Suffixes: Forgetting the L suffix on large integer values or f on float numbers causes compile-time syntax errors (`integer number too large` or `possible loss of precision`).
🎯 Tiered Practice Exercises
- Beginner: Declare variables representing a product's price, quantity, and in-stock boolean flag.
- Intermediate: Demonstrate the difference in precision between
floatanddoubleduring calculations. - Advanced: Explain how JVM Stack memory handles local primitive variable declarations.