Data Types in Java
1. Two Families (big picture)
Primitive (value in box: fast, lowercase) vs Reference (address of object: String, arrays, classes). Beginners master primitives first.
2. 8 Primitives (exam table — memorize size + example)
| Type | Size | Range / Use | Example |
|---|---|---|---|
byte |
1 byte | -128 to 127 | byte b = 100; |
short |
2 bytes | -32k to 32k | short s = 1000; |
int |
4 bytes | ~±2 billion (default integer) | int marks = 87; |
long |
8 bytes | huge (needs L) |
long pop = 1400000000L; |
float |
4 bytes | decimal (needs f) |
float pi = 3.14f; |
double |
8 bytes | decimal default | double x = 3.14159; |
char |
2 bytes | single Unicode char | char c = 'A'; |
boolean |
1 bit-ish | true/false only | boolean pass = true; |
Default: int for whole numbers, double for decimals. So long needs L, float needs f.
3. Code — See Defaults Bite
public class Types {
public static void main(String[] args) {
int marks = 87; // default int — fine
// float f = 3.14; // ERROR! 3.14 is double
float f = 3.14f; // ok with f
long big = 9000000000L; // needs L (bigger than int range)
char c = 'A';
System.out.println((int) c); // 65 — chars are numbers!
boolean pass = marks >= 40;
System.out.println(pass); // true
}
}
4. Reference Preview (next topics)
String name = "Asha"; int[] arr = {1,2}; — these hold addresses, default null, compare with .equals() (not ==).
5. Exam Points
- 8 primitives table + defaults (
L,f) — 5-7 marks, very frequent. - int vs long, float vs double traps — 3 marks, frequent.
- char as number (
(int)'A'= 65) — 2-mark favourite.