DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

4. Operators, Operands, Expression, Operator Precedence

Example of operators

        int result = 1 + 2; // addition
        result = result - 1; // subtraction
        result = result * 10; // multiplication
        result = result / 5; // division
        result = result % 3; // modulus
Enter fullscreen mode Exit fullscreen mode

Abbreviating of operators

        // result = result + 1;
        result++;

        // result = result - 1;
        result--;

        // result = result + 2;
        result += 2;

        // result = result * 10;
        result *= 10;

        // result = result / 3;
        result /= 3;

        // result = result % 2;
        result %= 2;
Enter fullscreen mode Exit fullscreen mode

Arithmetic operators

+, -, *, /, %, ++, --

Assignment operators

=, +=, -=, *=, /=, %=, &=, |=, ^=, >>=, <<=

Comparison operators

==, !=, >, <, >=, <=

Logical operators

&&, ||, !

Bitwise operators

&, |, ~, ^, <<, >>, >>>

Another operator is Ternary Operator

boolean loggedIn = false;
String userStatus = loggedIn ? "User is loggedIn" : "User is not logged in";
System.out.println(userStatus);
Enter fullscreen mode Exit fullscreen mode

More details can be found in
w3schools.com
oracle.com

Java Operator Precedence

Link

Top comments (0)