Primitive data types in Java store simple values directly in memory. They are not objects and do not have methods.
| Type | Size | Example Values | Notes |
|---|---|---|---|
int |
32-bit | -5, 0, 42 | Most commonly used for whole numbers |
double |
64-bit | 3.14, -0.5, 2.0 | Default type for decimals |
boolean |
Logical | true, false | Only two possible values |
char |
16-bit | 'A', '7', '$' | Single character, uses single quotes |
int, double, boolean, and char.
int number; double price; boolean isValid; char letter;
int number = 10; double price = 5.99; boolean isValid = true; char letter = 'A';
When both operands are int, the result is also an int.
Decimal values are truncated.
int result = 7 / 2; // result is 3
If at least one operand is a double, the result is a double.
double result = 7 / 2.0; // result is 3.5
Casting is the process of converting a value from one data type to another.
Implicit casting happens automatically when converting a smaller type to a larger type.
int x = 5; double y = x; // y becomes 5.0
Explicit casting is required when converting a larger type to a smaller type.
double x = 5.9; int y = (int) x; // y becomes 5
double to int
truncates the decimal value (does not round).
int a = 5; int b = 2; double result1 = a / b; // 2.0 double result2 = (double) a / b; // 2.5
charchar c = 'A'; int value = c; // 65 char next = (char)(value + 1); // 'B'
==int x = 10; int y = 3; double result = (double) x / y;
Answer: 3.3333333333333335