Java Operators

Operators do work on values and variables. Java has these groups.

Arithmetic operators

OperatorNameExampleResult
+Addition10 + 313
–Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (int division)
%Modulus (remainder)10 % 31
public class Main {
    public static void main(String[] args) {
        int a = 10, b = 3;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);      // int division
        System.out.println(a % b);      // remainder
        System.out.println(10.0 / 3);   // decimal division
    }
}
Output

13
7
30
3
1
3.3333333333333335

Assignment operators

public class Assign {
    public static void main(String[] args) {
        int x = 10;
        x += 5;     // x = x + 5
        System.out.println(x);
        x -= 3;
        System.out.println(x);
        x *= 2;
        System.out.println(x);
        x /= 4;
        System.out.println(x);
        x %= 4;
        System.out.println(x);
    }
}
Output

15
12
24
6
2

Increment and decrement

++ adds 1 and -- subtracts 1. The place of the operator matters: x++ (postfix) gives the old value first, ++x (prefix) changes it first.

public class Inc {
    public static void main(String[] args) {
        int x = 5;
        System.out.println(x++);   // prints 5, then x is 6
        System.out.println(x);
        System.out.println(++x);   // x becomes 7, then prints 7
        System.out.println(--x);   // 6
    }
}
Output

5
6
7
6

Comparison operators

They give true or false.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
public class Compare {
    public static void main(String[] args) {
        int a = 7, b = 10;
        System.out.println(a == b);
        System.out.println(a != b);
        System.out.println(a < b);
        System.out.println(a >= 7);
    }
}
Output

false
true
true
true

Logical operators

OperatorMeaning
&&AND: true if both are true
||OR: true if at least one is true
!NOT: reverses the value
public class Logic {
    public static void main(String[] args) {
        int age = 25;
        boolean hasId = true;
        System.out.println(age >= 18 && hasId);
        System.out.println(age < 18 || hasId);
        System.out.println(!hasId);
    }
}
Output

true
true
false

The logical operators are short circuit: in a && b, if a is false, Java does not even check b.

Ternary operator

A short form of if else: condition ? valueIfTrue : valueIfFalse.

public class Ternary {
    public static void main(String[] args) {
        int marks = 55;
        String result = marks >= 40 ? "Pass" : "Fail";
        System.out.println(result);
    }
}
Output

Pass

Bitwise operators

They work on the bits of a number: & (and), | (or), ^ (xor), ~ (not), << and >> (shift).

public class Bits {
    public static void main(String[] args) {
        int a = 6;   // 110
        int b = 3;   // 011
        System.out.println(a & b);    // 010 = 2
        System.out.println(a | b);    // 111 = 7
        System.out.println(a ^ b);    // 101 = 5
        System.out.println(a << 1);   // 1100 = 12
        System.out.println(a >> 1);   // 11 = 3
    }
}
Output

2
7
5
12
3

Operator precedence

Multiplication, division and remainder go before addition and subtraction. Use brackets to make it clear.

public class Order {
    public static void main(String[] args) {
        System.out.println(2 + 3 * 4);     // 14
        System.out.println((2 + 3) * 4);   // 20
    }
}
Output

14
20

Note: Do not compare two String values with ==. Use .equals(). You will see why in the next chapter. Next: Java Strings.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.