In Java, primitive data types are the most basic building blocks of data — they’re not objects, and they store actual values (not references). There are 8 primitive data types in Java, and each has a specific size and purpose.
Data Size Example
byte 1 byte (8-bit) 127
short 2 bytes (16-bit) 32000
int 4 bytes (32-bit) 100000
long 8 bytes (64-bit) 10000000000L
float 4 bytes (32-bit) 3.14f
double 8 bytes (64-bit) 3.1415926535
char 2 bytes (16-bit) 'A'
boolean 1 bit true or false
public class PrimitiveExample {
public static void main(String[] args) {
int age = 20;
float height = 5.9f;
char grade = 'A';
boolean isPassed = true;
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
}
}
Unlike primitive types (which store actual values like numbers or characters), non-primitive types store references to objects in memory.
String --> Sequence of characters
EX:"Hello", "Java
Array --> Stores multiple values of the same type
Ex:int[] nums = {1,2,3}
In simple terms:
A primitive is like the actual book.
A non-primitive is like the library card that points you to the book’s location.