Reading history

Java Operators: Arithmetic, Logical, Ternary and More

3 min read 0 views

Learn Java operators: arithmetic and integer division, ++ and --, assignment shortcuts, comparison, logical, ternary, bitwise and precedence.

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 / 5 is 3: dividing two integers drops the remainder. 17 % 5 gives that remainder, 2. Make one side a decimal (17.0 / 5) to get 3.4.
  • count += 5 is short for count = count + 5. The same works for -=, *=, /= and %=.
  • x++ gives the old value, then adds 1. ++x adds 1 first, then gives the new value. So y got 5 and z got 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 a boolean.
  • && (and), || (or) and ! (not) combine booleans. && and || short-circuit: they stop as soon as the answer is known.
  • The ternary operator condition ? a : b picks 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 * 4 is 14; brackets change the order.
  • &, |, ^, << and >> work on the individual bits of integers.

Precedence, Highest First

LevelOperators
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

  1. Expecting 7 / 2 to be 3.5. It is 3 with integers.
  2. Comparing strings with ==. Use a.equals(b).
  3. Writing = in a condition instead of ==. For int values this is a compile error, which is helpful.
  4. String + number order. "Sum: " + 2 + 3 prints Sum: 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; ++x increments then uses.
  • && and || short-circuit; ?: picks a value.
  • Compare strings with .equals(), not ==.

Next: Java if-else and switch.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *