Integer Division:
-
When two integers are divided, the decimal result is removed:
-
It gets trunicated, not rounded
- 5.3 and 5.7 would both be just 5
-
To keep the decimal, add a floating point number in the equation
-
String Arithmatic:
- Adding two String literals will simply combine both Strings instead of doing math
Order of Operations:
- Java follows normal mathmatical order of operations
Mixed Type Expressions:
-
Expressions comprised of the same types will have a result of that type:
-
int + int = int
-
double + double = double
-
-
When an expression contains multiple types, one type will have priority:
-
String > int, char, double, boolean
-
Double/Float > int
-
Assignment Conversion:
-
Happens automatically
-
Occurs during variable assignment (not in expression)
-
Can convert from smaller value to larger value but not the other way around
-
Not based off byte size but instead number size
-
Ex.
-
int ⇒ double = double
-
double ⇒ int = possible loss of precision error (can’t display decimals in int)
-
-
Casting:
-
Forcing Java to perform a type conversion using an operator
-
Cast operator has higher precedence than operator (PEMDAS)
-
Doesn’t cost possible loss of precision error
-
Place the cast operator in front of the type that is being converted
double avg = 4.0;
int gpa = (int)average;