Operators in Java

Operators in Java

Operators are used to performing arithmetic, logical or other operations on one or two operands in Java. They are of the following types:

    • Arithmetic Operators

    • Assignment Operators

    • Comparison Operators

    • Increment/Decrement Operators

    • Logical Operators

    • Bitwise Operators

    • Miscellaneous Operators

 

Arithmetic Operators in Java

Arithmetic operators are binary operators because they are applied to two operands and perform arithmetic operations on them.

Operator Name Description
+ Addition adds two operands of compatible type
Subtraction subtracts two operands of compatible type
* Multiplication multiplies two operands of compatible type
/ Division divides two operands of compatible type and returns the quotient
% Modulo returns remainder of a division done on two operands

Arithmetic Operators with Example:

//Java Program with arithmetic operators
public class ArithmeticOperators {
    public static void main(String[] args) {
        float var1=34.6f;
        float var2=67.2f;

        System.out.println("var1= "+var1+" and var2= "+var2);

        //Addition
        float var_add=var1+var2;
        System.out.println("var1+var2= "+var_add);

        //Subtraction
        float var_sub=var1-var2;
        System.out.println("var1-var2= "+var_sub);

        //Multiplication
        float var_mul=var1*var2;
        System.out.println("var1*var2= "+var_mul);

        //Division
        float var_div=var1/var2;
        System.out.println("var1/var2= "+var_div);

        //Modulo
        float var_mod=var1%var2;
        System.out.println("var1%var2= "+var_mod);
    }
}

Output:

var1= 34.6 and var2= 67.2
var1+var2= 101.799995
var1-var2= -32.6     
var1*var2= 2325.1199 
var1/var2= 0.51488096
var1%var2= 34.6

 

 

Assignment Operators in Java

Assignment operators are binary operators that assign value to the left-hand side operand the value of the right-hand side operand where the left-hand side is a variable that can store value in it.

Operator Expression Equivalent to
= x=1 x=1
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
&= x&=y x=x&y
|= x|=y x=x|y
^= x^=y x=x^y
>>= x>>=y x=x>>y
>>>= x>>>=y x=x>>>y
<<= x<<=y x=x<<y

Assignment Operators in with Example:

//Java program with assignment operators
public class AssignmentOperators {
    public static void main(String[] args) {
        float var=10f;

        var+=2;
        System.out.println("var+=2 makes var "+var);

        var-=34;
        System.out.println("var+=2 makes var "+var);

        var*=5;
        System.out.println("var+=2 makes var "+var);

        var/=7;
        System.out.println("var+=2 makes var "+var);

        var%=9;
        System.out.println("var+=2 makes var "+var);
    }
}

Output:

var+=2 makes var 12.0
var+=2 makes var -22.0     
var+=2 makes var -110.0    
var+=2 makes var -15.714286
var+=2 makes var -6.714286

 

 

Comparison Operators in Java

Comparison operators are binary operators that compare the value of two operands and return boolean values which can be either true or false depending on whether the comparison is correct or not.

Operator Description
== equal
!= not equal
> greater than
< less than
>= greater than or equal to
<= less than or equal to

Comparison Operators in with Example:

//Java Program with Comparison Operators
public class ComparisonOperators {
    public static void main(String[] args) {
        int var1=54;
        int var2=89;

        System.out.println("var1= "+var1+" and var2= "+var2);

        System.out.println("var1>var2 is "+(var1>var2));
        System.out.println("var1<var2 is "+(var1<var2));
        System.out.println("var1>=var2 is "+(var1>=var2));
        System.out.println("var1<=var2 is "+(var1<=var2));
        System.out.println("var1==var2 is "+(var1==var2));
        System.out.println("var1!=var2 is "+(var1!=var2));
    }
}

Output:

var1= 54 and var2= 89
var1>var2 is false 
var1<var2 is true  
var1>=var2 is false
var1<=var2 is true
var1==var2 is false
var1!=var2 is true

 

 

Increment/Decrement Operators in Java

They are unary operators as they work on a single operand and increase or decrease its value depending upon the operator by 1.

Operator Name Description
++x Pre-Increment  increments the value of x by 1 and then returns it
x++ Post-Increment returns the value of x and then increments it by 1
–x Pre-Decrement decrements the value of x by 1 and then returns it
x– Post-Decrement returns the value of x and then decrements it by 

 

Increment/Decrement Operators with Example:

//Java program with increment and decrement operators
public class IncrementDecrement {
    public static void main(String[] args) {
        int x=56;
        int y=29;

        System.out.println("x= "+x+" and y= "+y);
        //x=x+1 then z=x+1
        int z1=++x+y;
        System.out.println("x= "+x+" and y= "+y+" and z1= "+z1);

        //z=x=y then x=x+1
        int z2=x+++y;
        System.out.println("x= "+x+" and y= "+y+" and z2= "+z2);

        //x=x-1 then z=x-1
        z1=--x+y;
        System.out.println("x= "+x+" and y= "+y+" and z1= "+z1);

        z2=x--+y;
        System.out.println("x= "+x+" and y= "+y+" and z2= "+z2);


    }
}

Output:

x= 56 and y= 29
x= 57 and y= 29 and z1= 86
x= 58 and y= 29 and z2= 86
x= 57 and y= 29 and z1= 86
x= 56 and y= 29 and z2= 86

 

 

Logical Operators in Java

They are binary operators and are used to combine two or more conditions together and result in a boolean value that can either be true or false.

Operator Name Description
&& AND returns true only when all conditions are true
|| OR returns true when at least one condition is true
! NOT changes the boolean result of a condition from true to false or false to true

Logical Operators with Example:

//Java Program with logical operators
public class LogicalOperators {
    public static void main(String[] args) {
        int x=29;
        int y=45;

        System.out.println("(x>y)&&(x!=y) is "+((x>y)&&(x!=y)));
        System.out.println("(x>y)||(x!=y) is "+((x>y)||(x!=y)));
        System.out.println("!(x>y) is "+(!(x>y)));
    }
}

Output:

(x>y)&&(x!=y) is false
(x>y)||(x!=y) is true
!(x>y) is true

 

 

Bitwise Operators in Java

They are binary operators except for bitwise not operator and perform bitwise operations on operands.

Operator Name Description
& AND returns 1 if both bits at the same position in the two operands are 1
| OR returns 1 if any of the bits at the same position in the two operands is 1
^ XOR returns 1 if only one of the bits at the same position in the two operands is 1
~ NOT returns 1 if the bit at consideration is 0 and vice-versa
>> Right Shift number of bits in the left operand is moved to the right by the number of bits represented by the right operand
>>> Right Shift with Zero this works exactly like the right sift operator except that the bits shifted in on the left are always zero
<< Left Shift number of bits in the left operand is moved to the left by the number of bits represented by the right operand

 

 

Miscellaneous Operators in Java

Operator  Description
instanceof returns true if the object is of a specific type like a class or an interface; false otherwise
?: (ternary operator) evaluates a boolean expression and if true statement to the left of: is executed otherwise statement to the right of: is executed

 

Operators Precedence in Java

Precedence

Specifies the order in which operators are evaluated in an expression in Java.

Associativity

Specifies the left or right direction in which operators with the same precedence are evaluated.

 

 

Share this post

Leave a Reply

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