How to use operators
Use arithmetic operators
Java provides addition, subtraction, multiplication, division, and remainder operators.
int a = 10;
int b = 3;
System.out.println(a + b);
System.out.println(a / b); // 3
System.out.println(a % b); // 1
Avoid accidental integer division
Dividing two integers produces an integer. Convert an operand to double when a fractional result is needed.
double average = (double) 10 / 3;
System.out.println(average);
Compare values
Comparison operators produce booleans. Use ==, !=, <, <=, >, and >=.
int age = 20;
boolean adult = age >= 18;
boolean exact = age == 20;
Combine conditions
&& means and, || means or, and ! means not. The first two short-circuit when the result is already known.
boolean hasTicket = true;
int age = 17;
boolean canEnter = hasTicket && age >= 16;