Java Operators
Operators work on values. Arithmetic: + - * / %. Comparison: == != < > <= >=. Logical: && (and), || (or), ! (not).
ExampleJava
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); // 3 (integer division drops the decimals)
System.out.println(a % b); // 2 (remainder)
System.out.println((double) a / b); // 3.4
System.out.println(a > b && b > 0); // true
int x = 10;
x += 5; // x = x + 5
x++; // x = x + 1
System.out.println(x);
}
}Output
22 3 2 3.4 true 16
Dividing two int values gives an int. To keep the decimals, convert one of them to double first, as in (double) a / b.
Quick check
What is 17 / 5 when both are int?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.