Definition
📌 Data Types
Java defines exactly 8 primitive types with fixed sizes that never change across platforms — this consistency is part of what makes Java code portable..
Syntax
syntax
int wholeNumber;
double decimalNumber;
char singleCharacter;
boolean trueOrFalse;Example
java
public class Main {
public static void main(String[] args) {
int age = 20;
double gpa = 8.9;
char grade = 'A';
boolean passed = true;
System.out.println(age + " " + gpa + " " + grade + " " + passed);
}
}Output
output20 8.9 A true
Important Points
- Java has exactly 8 primitive types: byte, short, int, long, float, double, char, boolean.
booleancan only betrueorfalse— never an integer.intis 4 bytes,longis 8 bytes, always — regardless of the machine running it.- Non-primitive types (String, arrays, objects) are called reference types.
Common Mistakes
- Trying to assign an integer (like 1 or 0) directly to a
boolean— Java doesn't allow this. - Losing precision by assigning a
doublevalue into afloatorintwithout an explicit cast. - Confusing primitive
char(single character) withString(text).
Interview Tip
🎯 Before your interview
Interviewers often ask you to list Java's 8 primitive types from memory — it's worth memorizing them.
