Operators are the symbols that do work on values: + adds, > compares, && combines conditions. Most look like ordinary maths, but a few behave in ways that surprise beginners, especially integer division, ++ placement and comparing strings with ==. This lesson covers all the main groups with tested examples.
In this lesson you will learn
- Arithmetic operators and integer division
- Assignment shortcuts like
+= - The difference between
x++and++x - Comparison, logical and ternary operators
- Operator precedence
Arithmetic, Assignment and Increment
public class Main {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + b + " " + (a - b) + " " + a * b);
System.out.println(a / b + " " + a % b); // integer division
System.out.println(17.0 / 5);
int count = 10;
count += 5;
count *= 2;
System.out.println("count = " + count);
int x = 5;
int y = x++; // use, then increase
int z = ++x; // increase, then use
System.out.println(x + " " + y + " " + z);
}
}
Output
22 12 85
3 2
3.4
count = 30
7 5 7
17 / 5is 3: dividing two integers drops the remainder.17 % 5gives that remainder, 2. Make one side a decimal (17.0 / 5) to get 3.4.count += 5is short forcount = count + 5. The same works for-=,*=,/=and%=.x++gives the old value, then adds 1.++xadds 1 first, then gives the new value. Soygot 5 andzgot 7.
Comparison, Logical and Ternary
public class Main {
public static void main(String[] args) {
int marks = 72;
boolean passed = marks >= 40;
boolean scholarship = marks >= 80 && passed;
System.out.println(passed + " " + scholarship + " " + !passed);
String result = marks >= 40 ? "Pass" : "Fail"; // ternary
System.out.println(result);
String s1 = new String("java");
String s2 = "java";
System.out.println((s1 == s2) + " " + s1.equals(s2));
System.out.println(2 + 3 * 4 + " " + (2 + 3) * 4);
System.out.println(6 & 3); // bitwise AND: 110 & 011 = 010
}
}
Output
true false false
Pass
false true
14 20
2
- Comparison operators (
==,!=,<,>,<=,>=) return aboolean. &&(and),||(or) and!(not) combine booleans.&&and||short-circuit: they stop as soon as the answer is known.- The ternary operator
condition ? a : bpicks one of two values. - For objects such as strings,
==checks whether both names point to the same object..equals()compares the contents, which is almost always what you want. - Multiplication runs before addition, so
2 + 3 * 4is 14; brackets change the order. &,|,^,<<and>>work on the individual bits of integers.
Precedence, Highest First
| Level | Operators |
|---|---|
| 1 | ++ -- ! (unary) |
| 2 | * / % |
| 3 | + - |
| 4 | < > <= >= |
| 5 | == != |
| 6 | &&, then || |
| 7 | ?: |
| 8 | = += -= and other assignments |
When in doubt, add brackets. They cost nothing and make the intent clear.
Common Mistakes
- Expecting
7 / 2to be 3.5. It is 3 with integers. - Comparing strings with
==. Usea.equals(b). - Writing
=in a condition instead of==. Forintvalues this is a compile error, which is helpful. - String + number order.
"Sum: " + 2 + 3printsSum: 23; write"Sum: " + (2 + 3).
Practice Exercises
Exercise 1. Convert 135 minutes into hours and minutes.
Show answer
int total = 135;
System.out.println(total / 60 + " h " + total % 60 + " min"); // 2 h 15 min
Exercise 2. Use the ternary operator to print the larger of a and b.
Show answer
int a = 8, b = 13;
System.out.println(a > b ? a : b); // 13
Frequently Asked Questions
What is the difference between & and && in Java?
&& is the logical AND that skips the right side when the left side is false. & always evaluates both sides and also works as a bitwise AND on integers.
Does Java have a power operator?
No. Use Math.pow(2, 10), which returns a double. ^ is bitwise XOR, not a power.
Summary
- Integer division drops the remainder;
%returns it. x++uses then increments;++xincrements then uses.&&and||short-circuit;?:picks a value.- Compare strings with
.equals(), not==.
Next: Java if-else and switch.
