Operators
Arithmetic
public class Main {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3: int / int drops the remainder
System.out.println(a % b); // 2: the remainder
System.out.println(17.0 / 5); // 3.4
}
}When both sides of / are integers, the result is an integer, so 17 / 5 is 3. Make either side a double to keep the decimals.
% gives the remainder, which is more useful than it looks: n % 2 == 0 tests for an even number, and n % 10 is the last digit.
Compound assignment, ++ and --
public class Main {
public static void main(String[] args) {
int score = 10;
score += 5; // score = score + 5, so 15
score -= 3; // 12
score *= 2; // 24
score /= 4; // 6
score %= 4; // 2
System.out.println(score);
int count = 0;
count++; // 1
count++; // 2
count--; // 1
System.out.println(count);
int x = 5;
System.out.println(x++); // prints 5, then x becomes 6
System.out.println(++x); // x becomes 7, then prints 7
}
}x++ uses the current value and then adds one; ++x adds one first. On a line of their own they behave the same, but inside a larger expression they don't, which is why many style guides keep ++ on its own line.
Comparisons
public class Main {
public static void main(String[] args) {
int age = 20;
System.out.println(age == 20); // true
System.out.println(age != 18); // true
System.out.println(age > 21); // false
System.out.println(age <= 20); // true
}
}Comparisons produce a boolean. Remember that == compares and = assigns.
Logical operators
public class Main {
public static void main(String[] args) {
int age = 19;
boolean hasTicket = true;
System.out.println(age >= 18 && hasTicket); // AND: both must be true
System.out.println(age < 13 || age > 65); // OR: at least one must be true
System.out.println(!hasTicket); // NOT: flips the value
}
}&& and || short-circuit: when the left side already decides the answer, the right side never runs. That's what makes name != null && name.length() > 0 safe: if name is null, name.length() is never called.
Precedence
*, / and % come before + and -; comparisons come before &&, and && before ||. Use brackets whenever in doubt:
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4); // 14
System.out.println((2 + 3) * 4); // 20
System.out.println("Sum: " + 2 + 3); // Sum: 23
System.out.println("Sum: " + (2 + 3)); // Sum: 5
}
}+ joins text when either side is a String, working left to right. Once the left side is a String, every following + joins rather than adds, hence Sum: 23.
Overflow
public class Main {
public static void main(String[] args) {
int big = Integer.MAX_VALUE;
System.out.println(big); // 2147483647
System.out.println(big + 1); // -2147483648: it wrapped around
long fixed = (long) big + 1;
System.out.println(fixed); // 2147483648
}
}An int can't go past about 2.1 billion. Going over wraps around to a large negative number, silently. Use long for big values; Math.addExact(a, b) throws an exception instead of wrapping.
The Math class
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(4, 9)); // 9
System.out.println(Math.abs(-7)); // 7
System.out.println(Math.pow(2, 10)); // 1024.0
System.out.println(Math.sqrt(144)); // 12.0
System.out.println(Math.round(2.5)); // 3
System.out.println(Math.floor(2.9) + " " + Math.ceil(2.1)); // 2.0 3.0
System.out.println((int) (Math.random() * 6) + 1); // a dice roll, 1 to 6
}
}