Java Operators



In this tutorial, we will learn about different types of operators in Java, their syntax, and how to use them with the help of examples.


Java Operators

Operators are symbols that perform operations on variables and values.

For example, the + operator is used for addition, while the / is used for division.

Java operators can be classified into the following groups:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Unary Operators
  • Bitwise Operators

Java Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations on variables and values.

For example:

x + y;

Here, the + operator is used to add two variables, x and y.

There are different arithmetic operators in Java.

Operator Name Description Example
+ Addition Adds together two values a + b
- Subtraction Subtracts one value from another a - b
* Multiplication Multiplies two values a * b
/ Division Divides one value by another a / b
% Modulus Returns the division remainder a % b
++ Increment Increases the value of a variable by 1 ++a
-- Decrement Decreases the value of a variable by 1 --a

Example: Arithmetic Operators

public class Main { 
    public static void main(String[] args) {
 
        // declare variables
        int a = 14, b = 3;

        // addition operator
        System.out.println("a + b = " + (a + b));
        
        // subtraction operator
        System.out.println("a - b = " + (a - b));

        // multiplication operator
        System.out.println("a * b = " + (a * b));

        // division operator
        System.out.println("a / b = " + (a / b));

        // modulo operator
        System.out.println("a % b = " + (a % b));

        // increment operator
        System.out.println("++a = " + (++a));

        // decrement operator
        System.out.println("--a = " + (--a));
  }
}

Output:

a + b = 17
a - b = 11
a * b = 42
a / b = 4
a % b = 2
++a = 15
--a = 14

In the above example, we have used +, -, *, ++, and ** operators to perform addition, subtraction, multiplication, incrementation, and decrementation.

/ Division Operator

The operation a / b in the program above used the / division operator.

If we use the division operator with integers, the resulting will also be an integer. On the other hand, if one of the operands is a floating-point number, the result will also be a floating point.

public class Main { 
    public static void main(String[] args) {
      System.out.println("8 / 5 = " + (8 / 5));    
      System.out.println("8.0 / 5 = " + (8.0 / 5));
      System.out.println("8 / 5.0 = " + (8 / 5.0));
      System.out.println("8.0 / 5.0 = " + (8.0 / 5.0));
    }
}

Output:

8 / 5 = 1
8.0 / 5 = 1.6
8 / 5.0 = 1.6
8.0 / 5.0 = 1.6

% Modulo Operator

The modulo operator % returns the remainder. In the example above, when a=14 is divided by b=3, the remainder is 2.

Note: We generally use the % operator with integers.


Java Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we will use the assignment operator =, which assigns the value on its right to the variable on its left.

int x = 15;

Here we assign the value 15 to the x variable.

Java has different assignment operators.

Operator Example Equivalent to
= a = 9 a = 9
+= a += 9 a = a + 9
-= a -= 9 a = a - 9
*= a *= 9 a = a * 9
/= a /= 9 a = a / 9
%= a %= 9 a = a % 9
&= a &= 9 a = a & 9
|= a |= 9 a = a | 9
^= a ^= 9 a = a ^ 9
>>= a >>= 9 a = a >> 9
<<= a <<= 9 a = << 9

Example: Assignment Operators

public class Main {
    public static void main(String[] args) {
        // declare variables
        int a = 0;

        // assign value using =
        a = 2;
        System.out.println("The value of 'a' using =: " + a);

        // assign value using =+
        a += 2;
        System.out.println("The value of 'a' using +=:" + a);

        // assign value using =*
        a *= 2;
        System.out.println("The value of 'a' using *=:"+ a);
    }
}

Output:

The value of 'a' using =: 2
The value of 'a' using +=:4
The value of 'a' using *=:8

Java Comparison Operators

Comparison operators are used to compare two values.

Operator Name Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Example: Comparison Operators

public class Main {
    public static void main(String[] args) {
        // declare variables
        int a = 4, b = 9;
        
        // value of a and b
        System.out.println("a = " + a + " and b = " + b);
        
            // == operator
        System.out.println("(a == b) is " + (a == b));

        // != operator
        System.out.println("(a != b) is " + (a != b));

        // > operator
        System.out.println("(a > b) is " + (a > b));

        // < operator
        System.out.println("(a < b) is " + (a < b));

        // >= operator
        System.out.println("(a >= b) is " + (a >= b));

        // <= operator
        System.out.println("(a <= b) is " + (a <= b));
    }
}

Output:

a = 4 and b = 9
(a == b) is false
(a != b) is true
(a > b) is false
(a < b) is true
(a >= b) is false
(a <= b) is true

Note: Comparison operators are generally used in conditions and loops.


Java Logical Operators

Logical operators are used to define the logic between variables or values. They are generally used in decision-making.

Operator Name Description Example
&& Logical AND Returns true if both statement are true a < 10 && a > 3
|| Logical OR Returns true if one of the statement is true a > 10 || a > 6
! Logical NOT Reverse the result, returns false if the result is true !(a < 10 && a > 3)

Example: Logical Operators

public class Main {
    public static void main(String[] args) {
        
        // && operator
        System.out.println((6 > 2) && (12 > 7));    // true
        System.out.println((6 < 2) && (12 > 7));    // false

        // || operator
        System.out.println((6 < 2) || (12 > 7));       // true
        System.out.println((6 > 2) || (12 < 7));       // true
        System.out.println((6 < 2) || (12 < 7));       // false

        // ! operator
        System.out.println(!(7 == 2));       // true
        System.out.println(!(7 > 2));          // false
    }
}

The explanation of the above program can be given as follows:

  • (6 > 2) && (12 > 7) returns true because both (6 > 2) and (12 > 7) are true.
  • (6 < 2) && (12 > 7)) returns false because the expression (6 < 2) is false.
  • (6 < 2) || (12 > 7) returns true because the expression (12 > 7) is true.
  • (6 > 2) || (12 < 7) returns true because the expression (6 > 2) is true.
  • (6 < 2) || (12 < 7) returns false because both (6 < 2) and (12 < 7) are false.
  • !(7 == 2) returns true because 7 == 2 is false.
  • !(7 > 2) returns false because 7 > 2 is true.

Java Unary Operators

Unary operators are used with only one operand to perform any operation like increment, decrement, negation etc. For example, ++ is a unary operator that increases the value of a variable by 1.

Operator Name Description Example
+ Unary plus Not necessary to use since numbers are positive using it +7
- Unary minus It inverts the sign of an expression -7
++ Increment operator It increments value by 1 ++7
-- Decrement operator It decrements value by 1 --7
! Logical complement operator It inverts the value of a boolean !(5 > 3)

Increment and Decrement Operators

Java provides increment and decrement operators: ++ and -- respectively.

The increment operator ++ increases the value of the operand by 1, while the decrement operator -- decreases it by 1.

Example:

int x = 7

// increse x by 1
++x;   

Above, the x value gets increased to 8 from its initial value of 7.


Example: Increment and Decrement Operators

public class Main {
    public static void main(String[] args) {
        // declare variables
        int a = 7, b = 7;
        int resultA, resultB;

        System.out.println("Value of a: " + a);
        
        // increment operator
        resultA = ++a;
        System.out.println("After increment: " + resultA);

        System.out.println("Value of b: " + b);

        // decrement operator
        resultB = --b;
        System.out.println("After decrement: " + resultB);
    }
}

Output:

Value of a: 7
After increment: 8
Value of b: 7
After decrement: 6

Above, we have used the ++ and -- operator as prefixes (++a, --b). But we can also use these operators as postfix (a++, b--).

The difference when these operators are used as prefix versus when they are used as postix:

  • If we use the ++ operator as a prefix like: ++a, first the value of a is incremented by 1; then returns the value.
  • If we use the ++ operator as a postfix like: --a, first the original value of a is returned, then the a is incremented by 1.

Note: the -- operator operates similarly to the ++ operator except -- decreases the value by 1.


Java Bitwise Operators

Bitwise operators are used to perform operations on individual bits.

Example:

Bitwise complement Operation of 37

37  = 00100101 (In Binary)

~ 00100101 ==> 11011010 = 218 (In decimal)

Above, the bitwise operator ~ inverts the value of each bit (0 to 1 and 1 to 0).

The different bitwise operators present in Java are:

Operator Description
~ Bitwise complement
<< Left shift
>> Right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR

Other operators

In addition to the above operators, there are other additional operators in Java.


Java instanceof Operator

The instanceof operator checks whether an object is an instance of a particular class.

Example:

public class Main {
    public static void main(String[] args) {
        String text = "Hello World";
        boolean result;

        result = text instanceof String;
        System.out.println("Is text an object of String? " + result);
    }
}

Output:

Is text an object of String? true

Here above, the text is an instance of the String class. This is why, the ìnstanceof operator returns true.


Java Ternary Operator

The ternary operator is shorthand for the if-then-else statement.

For example:

var = Expression ? expression1 : expression 

The ternary operator works as follows:

  • If the Expression is true, then expression1 is assigned to the var.
  • If the Expression is false, then expression2 is assigned to the var.

Let us see an example of a ternary operator.

public class Main {
    public static void main(String[] args) {
        int num = 17;
        String result;

        result = (num > 10) ? "Number is greater than 10" : "Number is less than or eaual to 10";
        System.out.println(result);
    }
}

Output:

Number is greater than 10

Here, we used the ternary operator to check if the number is greater than 10 or not.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.