Operators in Java Explained

Operator are those who performs an action, for example + is an arithmetic operator that perform addition. They are various types of operators are available in java programming language.

Types of Operator in Java

  • Arithmetic Operators
  • Assignment Operators
  • Auto-increment and Auto-decrement Operators
  • Logical Operators
  • Comparison (relational) operators

Arithmetic Operators

arithmetic operators are: +, -, *, /, %

+ is used for addition

– is used for substraction

* is for multiplication

/ is used for division

% is used for find reminder, % is called modulus(modulo) operator

Let’s take an example:

package com.sync;

public class OperatorsDemo {

    public static void main(String[] args) {

        int num1 = 300;
        int num2 = 30;

        System.out.println("num1 + num2: " + (num1 + num2) ); // addition
        System.out.println("num1 - num2: " + (num1 - num2) ); // subtraction
        System.out.println("num1 * num2: " + (num1 * num2) ); // multiplication
        System.out.println("num1 / num2: " + (num1 / num2) ); // division
        System.out.println("num1 % num2: " + (num1 % num2) ); // modulo
    }
}

/* output: 

 num1 + num2: 330
 num1 - num2: 270
 num1 * num2: 9000
 num1 / num2: 10
 num1 % num2: 0 

*/

Assignment Operators

Assignments operators in java are: =, +=, -=, *=, /=, %=

  • num2 = num1 would assign value of variable num1 to the variable num2.
  • num2+=num1 is equal to num2 = num2+num1
  • num2-=num1 is equal to num2 = num2-num1
  • num2*=num1 is equal to num2 = num2*num1
  • num2/=num1 is equal to num2 = num2/num1
  • num2%=num1 is equal to num2 = num2%num1

So, let’s take an example:

package com.sync;

public class OperatorsDemo {

    public static void main(String[] args) {

        int num1 = 10;
        int num2 = 20;

        num2 = num1;
        System.out.println("= Output: "+num2);

        num2 += num1;
        System.out.println("+= Output: "+num2);

        num2 -= num1;
        System.out.println("-= Output: "+num2);

        num2 *= num1;
        System.out.println("*= Output: "+num2);

        num2 /= num1;
        System.out.println("/= Output: "+num2);

        num2 %= num1;
        System.out.println("%= Output: "+num2);

    }
}
/* output:

 = Output: 10
 += Output: 20 
 -= Output: 10
 *= Output: 100
 /= Output: 10
 %= Output: 0

*/

Auto-increment and Auto-decrement Operators

++ and — are called Auto-increment and Auto-decrement Operators.

  • num++ is equivalent to num=num+1;
  • num–- is equivalent to num=num-1;

So, let’s take an example:

package com.sync;

public class OperatorsDemo {

    public static void main(String[] args) {

        int num1=100;
        int num2=200;
        System.out.println("Before Increment num1 is: " + num1);
        System.out.println("Before Decrement num2 is: " + num2);
        num1++;
        num2--;
        System.out.println("After Increment num1++ is: "+num1);
        System.out.println("After decrement num2-- is: "+num2);

    }
}
/* output 

 Before Increment num1 is: 100
 Before Decrement num2 is: 200
 After Increment num1++ is: 101
 After decrement num2-- is: 199

*/

Logical Operators

They’re mostly utilized to evaluate a condition in conditional expressions and loops.

&&, ||, and ! are logical operators used in Java.

Consider the following scenario: we have two boolean variables, b1 and b2.

  • If both b1 and b2 are true, b1&&b2 will return true; otherwise, it will return false.
  • If both b1 and b2 are false, b1||b2 will return false; otherwise, it will return true.
  • !b1 would return the inverse of b1, meaning it would return true if b1 is false and false if b1 is true.
package com.sync;

public class OperatorsDemo {

    public static void main(String[] args) {


        boolean b1 = true;
        boolean b2 = true;

        System.out.println("b1 && b2: " + (b1&&b2));
        System.out.println("b1 || b2: " + (b1||b2));
        System.out.println("!(b1 && b2): " + !(b1&&b2));


        boolean c1 = false;
        boolean c2 = true;

        System.out.println("c1 && c2: " + (c1&&c2));
        System.out.println("c1 || c2: " + (c1||c2));
        System.out.println("!(c1 && c2): " + !(c1&&c2));

        boolean d1 = true;
        boolean d2 = false;

        System.out.println("d1 && d2: " + (d1&&d2));
        System.out.println("d1 || d2: " + (d1||d2));
        System.out.println("!(d1 && d2): " + !(d1&&d2));

        boolean e1 = false;
        boolean e2 = false;

        System.out.println("e1 && e2: " + (e1&&e2));
        System.out.println("e1 || e2: " + (e1||e2));
        System.out.println("!(e1 && e2): " + !(e1&&e2));

    }
}

/* Output:

 b1 && b2: true
 b1 || b2: true
 !(b1 && b2): false

 c1 && c2: false
 c1 || c2: true
 !(c1 && c2): true

 d1 && d2: false
 d1 || d2: true
 !(d1 && d2): true

 e1 && e2: false
 e1 || e2: false
 !(e1 && e2): true

*/

Comparison(Relational) operators

==, !=, >, <, >=, <= are the relation operators in java.

  • If both the left and right sides are equal, == returns true.
  • If the left and right sides of the operator are not equal,!= returns true.
  • If the left side is greater than the right, > returns true.
  • If the left side is less than the right side, this function returns true.
  • If the left side is greater than or equal to the right side, >= returns true.
  • If the left side is smaller than or equal to the right side, then = returns true.
package com.sync;

public class OperatorsDemo {

    public static void main(String[] args) {      
 
        int num1 = 10;
        int num2 = 50;
        if (num1==num2) {
            System.out.println("num1 and num2 are equal");
        }
        else{
            System.out.println("num1 and num2 are not equal");
        }

        if( num1 != num2 ){
            System.out.println("num1 and num2 are not equal");
        }
        else{
            System.out.println("num1 and num2 are equal");
        }

        if( num1 > num2 ){
            System.out.println("num1 is greater than num2");
        }
        else{
            System.out.println("num1 is not greater than num2");
        }

        if( num1 >= num2 ){
            System.out.println("num1 is greater than or equal to num2");
        }
        else{
            System.out.println("num1 is less than num2");
        }

        if( num1 < num2 ){
            System.out.println("num1 is less than num2");
        }
        else{
            System.out.println("num1 is not less than num2");
        }

        if( num1 <= num2){
            System.out.println("num1 is less than or equal to num2");
        }
        else{
            System.out.println("num1 is greater than num2");
        }

    }
}
/* Output:
 num1 and num2 are not equal
 num1 and num2 are not equal
 num1 is not greater than num2
 num1 is less than num2
 num1 is less than num2
 num1 is less than or equal to num2
*/

Hope you got clarity