Operators in Java

Operators in Java
Operators in Java 1

operators are present every programming language, operators are single character which does some specific task .for example + operator is used to add two numbers

Types of Operators in Java

  1. Arithmetic Operators
  2. Logical Operators
  3. Auto-increment and Auto-decrement Operators
  4. Comparison (relational) operators
  5. Bitwise Operators
  6. Assignment Operators
  7. Ternary Operator

Arithmetic Operators

Arithmetic operators are used to performing arithmetic operations like addition subtraction.

NOsymbolname
1+addition
2subtraction
3*multiplication
4/division
5%modulo

Modulo operator returns remainder, for example, 10 % 5 would return 0

Example for Arithmetic Operators

public class xx{
    public static void main(String args[]) {
       int x = 100;
       int y = 50;
 
       System.out.println("x + y: " + (x + y) );
       System.out.println("x - y: " + (x - y) );
       System.out.println("x * y: " + (x * y) ); 
       System.out.println("x / y: " + (x / y) );
       System.out.println("x % y: " + (x % y) );
    }
 }

OUTPUT

x + y: 150
x - y: 50
x * y: 5000
x / y: 2
x % y: 0

Logical Operators

Using logical operators we can check multiple conditions .this condition has two answers true or false ,for example “3 is odd or even number” the answer is true .there are so many multiple conditions in logical operators

operator meaningExample
&&Logical ANDIf c=1 and d=2 then,
(c==1) && (d>1) equals to 0
!Logical NOT
If c =1 then, expression ! (c
== 1) equals to 0.
||Logical ORIf c=1 and d=2 then,
(c==1) || (d>1) equals to 1.

if there are two conditions .the logical operators works as shown in below table

LOGICAL OPERATORFIRST CONDITION SECOND CONDITIONRESULT
&&truetruetrue
&&truefalsefalse
&&falsetruefalse
&&falsefalsefalse
||truetruetrue
||truefalsetrue
||falsetruetrue
||falsefalsefalse

Logical Operators Example

public class logical{
    public static void main(String args[]) {
       boolean x = true;
       boolean y = false;
 
       System.out.println("x && y: " + (x&&y));
       System.out.println("x || y: " + (x||y));
       System.out.println("!(x && y): " + !(x&&y));
    }
 }

OUTPUT

x && y: false
x || y: true
!(x && y): true

Auto-increment and Auto-decrement Operators

these operators are used to increment a value by 1 (++) and it uis used to decrement a value by 1 by (–)

increment operator is implemented in two forms

1)pre-increment

here, the value incremented first and and then assigned

example: ++a

a = 5;  
b =++a;
result 
a=6
b=6

2)post-increment

here, the value is assigned first and the incremented

ja = 5;  
b =a++;
result 
a=6
b=5

3)pre-decrement

here, the value decremented first and and then assigned

 a = 5;  
b =--a;
result 
a=4
b=4

4 )post-decrement

here, the value is assigned first and then decremented

a = 5;  
b =a--;
result 
a=4
b=5

Increment Decrement Example:

public class inde{
    public static void main(String args[]) {
       int a=4,b=6,c=34,d=12;
       System.out.println(a++);
       System.out.println(++b);
       System.out.println(c--);
       System.out.println(--d);
    }
 }

OUTPUT

4
7
34
11

Relational operators

Relational operators are used to comparing between two values .it evaluates to 1 if the condition is true and 0 for if it is false.

NOsymbolmeaning
1==returns true if both the left side and right side are equal
2!=returns true if the left side is not equal to the right side of the operator.
3>returns true if the left side is greater than right.
4<returns true if the left side is less than the right side.
5>=returns true if the left side is greater than or equal to the right side.
6<=returns true if the left side is less than or equal to the right side.

Relational operators Example

public class Main{

	public static void main(String[] args) {

		int a = 10;
		int b = 20;

		System.out.println(a == b);
		System.out.println(a != b);
		System.out.println(a > b);
		System.out.println(a < b);
		System.out.println(a >= b);
		System.out.println(a <= b);

	}

}

OUTPUT

false                                                                                                                                          
true                                                                                                                                           
false                                                                                                                                          
true                                                                                                                                           
false                                                                                                                                          
true     

Bitwise Operators

these are used to perform calculations on binary numbers. addition, subtraction, addition, and division are done in bit-level which makes processing faster and saves power

NOoperatormeaning
1&Bitwise AND
2^Bitwise exclusive OR
3|Bitwise OR
4~Bitwise complement
5>> and <<Shift right and left

Bitwise AND operator &

the output of bitwise AND will be 1,if the both operands are 1 ,if either any one is 0 , the result of corresponding to that bit is evaluated to 0.

a = 5 , b=10
c=a&b
a = 5 = 0101
b = 10= 1010
        0000
c=0000=20

Bitwise OR operator |

this will return 1 ,when any one of the input is 1,otherwise returns zero.

a = 5 , b=10
c=a|b
a = 5 = 0101
b = 10= 1010
        1111
c=1111=15=F

Bitwise XOR (exclusive OR) operator ^

this returns 1,when the two operands are diffrent ,but it both are same it returns 0

a = 5 , b=10
c=a^b
a = 5 = 0101
b = 10= 1010
        1111
c=1111=15=F

Bitwise complement operator ~

this is an uniary operator,works on one operand . It changes the 1 to 0 and 0 to 1. It is denoted by ~.

a = 0101
~a =1010
   =1010
a=value (10)

Right Shift Operator >>

this is used to shift the bits in a given binary number towards right in a required no of positions ,denoted by >>

left shit operator <<

Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.

Assignment Operators

OperatorStep 1 exampleStep 2 example
%=a %= ba = a%b
*=a *= ba = a*b
+=a += ba = a+b
-=a -= ba = a-b
/=a /= ba = a/b
=a = ba = b

Assignment Operators Example


class assignmentoperators
{
                  public static void main(String args[])
         {
                    int X,Y;
                    X=10;
                    Y=13;
                    System.out.println("X += 3 : "+(X+=3));
                    System.out.println("X -= 11 : "+(X-=11));
                    System. out. println("Y *= 8 : "+(Y*=8));
                    System. out. println("Y /= 6 : " +(Y/=6));
          }
}

Output

X += 3 : 13
X -= 11 : 2
Y *= 8 : 104
Y /= 6 : 17

Ternary Operator

This operator has three arguments ,the value will be returned based on boolean expression

Syntax:

Boolean Expression ? First Statement : Second Statement

if the boolean expression is true then First statement will be returned and if the boolean expression is false then Second statement will be returned

Ternary operator can be replaced with if-else. The below example can be written using if-else statement as shown below.

Ternary Operator Example


class ifelse
{
                  public static void main(String args[])
         {
            int x = 20, y = 10;
            int result = 0;
            System. out. println("the first number is " +x);
            System. out. println("the second number is " +y);
            if (x > y)
                result = x;
            else if (x < y)
                result = y;
            System. out. println("the biggest number is " +result);
          }
}

This can be replaced by the below code


class ternanry
{
                  public static void main(String args[])
         {
            int x = 20, y = 10;
            int result = 0;
            System. out. println("the first number is " +x);
            System. out. println("the second number is " +y);
            result = x>y?x:y;
            System. out. println("the biggest number is " +result);
          }
}

Output

the first number is 20
the second number is 10
the biggest number is 20

Default image
Vishal Devxo
Vishal is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python
Leave a Reply