The operators in java is a character that is used to perform operations or actions. For example, “+” is an arithmetic operator used to add numbers.

Types of operators in Java

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

Arithmetic Operators

Arithmetic operators such as +, -, *, / are used for counting verbs such as addition, subtraction, multiplication, division, etc.

The “+” operator is used to add numbers.
The “-” operator is used to subtract numbers.
The “*” operator is used to multiply numbers.
The “/” operator is used for part of numbers

ListBox Control in ASP.Net: The Most Innovative Things

Examples

public class ArithmeticOperatorDemo {
  public static void main(String args[]) {
   int num1 = 100;
   int num2 = 20;

   System.out.println("num1 + num2: " + (num1 + num2));
   System.out.println("num1 - num2: " + (num1 - num2));
   System.out.println("num1 * num2: " + (num1 * num2)); 
   System.out.println("num1 / num2: " + (num1 / num2));
   
  }
}

Output

num1 + num2: 120
num1 – num2: 80
num1 * num2: 2000
num1 / num2: 5

Assignment Operators

Operators like =, + =, – =, * =, / =,% = are placed in the category of Assignment Operators.

num2 = num1: here the value of = operator variable num1 will be assigned to the value num2 variable.
num2 + = num1: this is equal to num2 = num2 + num1.
num2- = num1: this is equal to num2 = num2-num1.
num2 * = num1: this is equal to num2 = num2 * num1.
num2 / = num1: this is equal to num2 = num2 / num1.
num2% = num1: this equals num2 = num2% num1.

Examples

public class AssignmentOperatorDemo {
  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

The “++” operator is the auto-increment operator and the – is the auto-decrement operator.
num ++ is equal to num = num + 1.
num–- num = equal to num-1.

Examples

public class AutoOperatorDemo {
  public static void main(String args[]){
   int num1=100;
   int num2=200;
   num1++;
   num2--;
   System.out.println("num1++ is: "+num1);
   System.out.println("num2-- is: "+num2);
  }
}

Output

num1 ++ is: 101
num2– is: 199

Logical Operators

The logical operator is used with the binary variable. These operators are mainly used to evaluate conditions in conditional statements and loops. && in Java, || ,! Such operators are used as logical operators.

If we have 2 variables b1 and b2 then –

b1 && b2 Here if the value of both b1 and b2 is true, it will return the && operator “true”.
b1 || b2 Here if the values ​​of b1 and b2 are false then “||” The operator will return “false”.
! b1 Here if the value of b1 is true then “!” The operator will return false and if the value of b1 is false then “!” True return by operator

Examples

public class LogicalOperatorDemo {
  public static void main(String args[]) {
   boolean b1 = true;
   boolean b2 = false;

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

Output

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

Comparison (relational) operators

Java has 6 types of relational operators – ==,! =,>, <,> =, <=

== If both the left and right sides are equal then this operator will return true.
! = If the left (left) side and the right (right) side are not equal, then this operator will return true.
> If the left (left) side is larger than the right (right) side, then this operator will return true.
> = If the left (left) side is greater than or equal to the right (right) side, then this operator will return true.
<= If the left (left) side is smaller than or equal to the right (left) side, then this operator will return true.

Examples

public class RelationalOperatorDemo {
  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

Note: – If-else statement is used in this example, the if-else statement will be explained in the following notes.

Bitwise operators

In Java, 6 types of operators are categorized as Bitwise Operators – &, |, ^, ~, <<, >>

num1 = 11; This is equal to 00001011.
num2 = 22; This is equal to 00010110.

These operator perform bit by bit processing.

num1 & num2: – Here & operator compares the corresponding bits of num1 and num2 and if both have same bits then output gives 1 otherwise 0 returns output. In our case the output will be 2 because only the second bit from the end is being matched in binary form of num1 and num2.

num1 | num2: – here | The operator compares the corresponding bits of num1 and num2 and if any one bit is 1 then output gives 1 otherwise 0 outputs. In our case the output will be 31 which is 00011111.

num1 ^ num2: – Here the ^ operator compares the corresponding bits of num1 and num2 if they are not equal, the output gives 1 otherwise 0 gives the output. In our case the output will be 29 which is equal to 00011101.

~ num1: – ~ operator called a compliment operator which changes the bit from 0 to 1 and 1 to 0. In our example the output will be -12 which is equal to 11110100.

num1 << 2: – << operator is a left shift operator which moves the bits to the left side, and gives the rightest bit a value of 0. In our example the output is 44 which is equal to 00101100.

num1 >> 2: – >> operator is a right shift operator which moves the bits to the right side, and gives the leftest bit a value of 0. In our example the output is 2 which is equal to 00000010.

Examples

public class BitwiseOperatorDemo {
 public static void main(String args[]) {

   int num1 = 11; /* 11 = 00001011 */
   int num2 = 22; /* 22 = 00010110 */
   int result = 0;

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

   result = num1| num2;  
   System.out.println("num1| num2: "+result);
  
   result = num1 ^ num2;  
   System.out.println("num1 ^ num2: "+result);
  
   result = ~num1;  
   System.out.println("~num1: "+result);
  
   result = num1 << 2;  
   System.out.println("num1 << 2: "+result); result = num1 >> 2;  
   System.out.println("num1 >> 2: "+result);
 }
}

Output

num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~ num1: -12
num1 << 2: 44 num1 >> 2: 2

Ternary Operator

This operator evaluates boolean expression and assigns value based on the result.
Syntax: –
variable num1 = (expression)? value if true: value if true

If the result of the expression is true then the value before: is assigned to num1 otherwise: the value after: is assigned to num1.

Examples

public class TernaryOperatorDemo {
  public static void main(String args[]) {
    int num1, num2;
    num1 = 25;
    /* num1 is not equal to 10 that's why
	 * the second value after colon is assigned
	 * to the variable num2
	 */
	num2 = (num1 == 10) ? 100: 200;
	System.out.println("num2: "+num2);

	/* num1 is equal to 25 that's why
	 * the first value is assigned
	 * to the variable num2
	 */
	num2 = (num1 == 25) ? 100: 200;
	System.out.println("num2: "+num2);
  }
}

Output

num2: 200
num2: 100

Operator Precedence in Java

This determines if the expression has more than one operator, which operator needs to be evaluated first and which operator to evaluate later.

Unary Operators
++ – -! ~
Multiplicative
* /%
Additive
+ –
Shift
<< >> >>>
Relational
>> = <<=
Equality
==! =
Bitwise AND
&
Bitwise xor
^
Bitwise OR
|
Logical AND
&&
Logical OR
||
Ternary
?:
Assignment
= + = – = * = / =% =>> = <<= & = ^ = | =

.

LEAVE A REPLY

Please enter your comment!
Please enter your name here