Primitive Types vs Reference Types
Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Primitive variables hold values according to the language's rules. Reference variables hold references to objects, arrays, or null.
int is the usual integral type. Use long for larger integer ranges, double for general floating-point calculations, and boolean for true/false state. char represents a UTF-16 code unit.
int count = 42;
long population = 8_000_000_000L;
double price = 19.95;
boolean active = true;
char grade = 'A';
String name = "Suriya";
int[] scores = {90, 85, 95};
Wrapper classes such as Integer, Long, and Boolean represent primitive values as objects and are required in many generic APIs.
Key Takeaways
- Primitive and reference types have different semantics.
Stringand arrays are reference types.- Choose numeric types based on range and precision requirements.