Primitive Types
What is a primitive?
Primitive types are the most basic data types in Java. They store single, simple values (not objects) and are stored directly in memory.
Integers (int) store whole numbers (ex: 5, -3)
Doubles (double) store decimal numbers (ex: 3.14)
Booleans (boolean) store true/false values (ex: true, false)
Example:
int a = 10;
double b = 2.5;
boolean isValid = true;
Java supports arithmetic operations:
+ (addition), - (subtraction), * (multiplication), / (division), % (remainder)
Example:
int x = 7;
int y = 3;
System.out.println(x + y); // 10
7+3=10
System.out.println(x / y); // 2
7/3=2.333, but since integer division, the number is rounded down.
System.out.println(x % y); // 1
Remainder of 7/3 is 1.
Order of operations:
(), *, /, %, +, -
IMPORTANT: Integer division truncates decimals.
Example:
System.out.println(7 / 2); // 3
To get a decimal:
System.out.println(7 / 2.0); // 3.5
If you want a decimal answer, at least one number must be a double. For example, 7 divided by 2.0 would give 3.5.
Casting converts types:
double num = 5.7;
int val = (int) num;
Casts the double into an int. val = 5.