Variables & Data Types
In Java, every variable has a type — like int (whole number), double (decimal number), char (single letter), boolean (true/false), and String (text).
Java is a "statically typed" language — meaning you have to declare the box's type upfront, and only that type of thing will fit in that box.
int age = 10;
double price = 99.5;
char grade = 'A';
boolean isFun = true;
String name = "Duke";- Variable = a labeled box for data
- Types: int, double, char, boolean, String
- Java is statically typed
Java has 8 primitive types, which can be split into two groups: numbers (byte, short, int, long, float, double), text (char), and logic (boolean). Every type has its own fixed size (bits/bytes), which decides how big a number it can hold.
For whole numbers there are 4 types — from small to large: byte, short, int, long. int is the default choice in most programs (long for very large numbers). For decimals there's float and double — double is more precise, and decimal literals (like 3.14) are treated as double by default in Java, which is why you need the 'f' suffix to use float.
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
| byte | 1 byte | -128 to 127 | 0 | byte b = 100; |
| short | 2 bytes | -32,768 to 32,767 | 0 | short s = 2000; |
| int | 4 bytes | ~-2.1B to 2.1B | 0 | int i = 50000; |
| long | 8 bytes | Very large | 0L | long l = 100L; |
| float | 4 bytes | ~7 decimal digits precision | 0.0f | float f = 3.14f; |
| double | 8 bytes | ~15 decimal digits precision | 0.0 | double d = 3.14; |
| char | 2 bytes | 0 to 65,535 (Unicode) | '\u0000' | char c = 'A'; |
| boolean | 1 bit (JVM-dependent) | true / false | false | boolean b = true; |
A local variable is defined inside a method/block — it's "alive" only there (once the method ends, so does the variable). It must be initialized before use, or you'll get a compile error — Java gives local variables no default value (deliberately, so uninitialized data is never used by mistake).
An instance variable is defined inside a class, outside any method — every object gets its own separate copy. It's stored in "heap" memory along with the object, and automatically gets a default value (0, null, false) if you don't explicitly initialize it.
A static variable is tied to the class, not to any individual object — all objects share a single copy. It's created the moment the program starts (when the class loads), and lasts for the whole program.
class Counter {
int id; // instance variable — har object ki apni
static int total; // static variable — sabme shared
void increment() {
int temp = 1; // local variable — sirf is method mein
total += temp;
}
}Java uses camelCase for variables/methods (studentName, calculateTotal), and PascalCase for class names (StudentRecord, BankAccount). Constants (static final variables) are written in SCREAMING_SNAKE_CASE (MAX_SPEED, PI_VALUE) — these conventions aren't official in the Java Language Specification, but the whole industry follows them, so breaking them makes code look "unprofessional".
For numeric literals, Java 7+ lets you use underscores for readability — 1_000_000 is much easier to read than 1000000. Hexadecimal starts with 0x (0x1A = 26), octal with just 0 (010 = 8 in decimal), and binary with 0b (0b1010 = 10).
Variable names can contain letters, digits, underscore (_), and dollar sign ($), but can't start with a digit, and can't use Java's reserved keywords (class, int, if, etc.).
int million = 1_000_000;
int hex = 0x1A; // 26
int binary = 0b1010; // 10
final double MAX_SPEED = 120.0;