Operators in java

Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in java
  • Unary Operator
  • Arithmetic Operator
  • Shift Operator
  • Relational Operator
  • Bitwise Operator
  • Logical Operator
  • Ternary Operator
  • Assignment Operator
Operators in java
Java Operator Precedence
Operator TypeCategoryPrecedence
Unarypostfixexpr++ expr--
prefix++expr --expr +expr -expr ~ !
Arithmeticmultiplicative* / %
additive+ -
Shiftshift<< >> >>>
Relationalcomparison< > <= >= instanceof
equality== !=
Bitwisebitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
Logicallogical AND&&
logical OR||
Ternaryternary? :
Assignmentassignment= += -= *= /= %= &= ^= |= <<= >>= >>>=
Arithmetic Operators in Java

Java Arithmetic operators are used for simple math operations, they are

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (%)
Java Arithmetic Operator Example
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) ); System.out.println("num1 % num2: " + (num1 % num2) ); } }


Output:
num1 + num2: 120 num1 - num2: 80 num1 * num2: 2000 num1 / num2: 5 num1 % num2: 0
Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:

  • incrementing/decrementing a value by one
  • negating an expression
  • inverting the value of a boolean
num++ is equivalent to num=num+1; num–- is equivalent to num=num-1;
Java Unary Operator Example: ++ and --
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
class OperatorExample { public static void main(String args[]) { int x=10; System.out.println(x++);//10 (11) System.out.println(++x);//12 System.out.println(x--);//12 (11) System.out.println(--x);//10 } }


Output:
10 12 12 10
class OperatorExample{ public static void main(String args[]){ int a=10; int b=10; System.out.println(a++ + ++a);//10+12=22 System.out.println(b++ + b++);//10+11=21 }}


Output:
22 21
Java Unary Operator Example: ~ and !
class OperatorExample { public static void main(String args[]) { int a=10; int b=-10; boolean c=true; boolean d=false; System.out.println(~a);//-11 (minus of total positive value which starts from 0) System.out.println(~b);//9 (positive of total minus, positive starts from 0) System.out.println(!c);//false (opposite of boolean value) System.out.println(!d);//true } }


Output:
-11 9 false true
Assignment Operators

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

num2 = num1 would assign value of variable num1 to the variable.

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
Java Assignment Operator Example
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
class OperatorExample{ public static void main(String args[]){ int a=20; int b=30; a+=4;//a=a+4 (a=20+4) b-=4;//b=b-4 (b=30-4) System.out.println(a); System.out.println(b); }}


Output:
24 26
Java Assignment Operator Example: Adding short
class OperatorExample{ public static void main(String args[]){ short a=20; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 20+10=30 now int System.out.println(a); }}


Output:
Compile time error
Shift Operator
Operator Name Description
== (equals to) ex. x==y True if x equals y, otherwise false
!= (not equal to) ex. x!=y True if x is not equal to y, otherwise false
< (less than) ex. x<y True if x is less than y, otherwise false
> (greater than) ex.x > y True if x is greater than y, otherwise false
>= (greater than or equal to) ex. x>=y True if x is greater than or equal to y, otherwise false
<= (less than or equal to) ex. x<=yTrue if x is less than or equal to y, otherwise false
Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.

class OperatorExample { public static void main(String args[]) { System.out.println(10>>2);//10/2^2=10/4=2 System.out.println(20>>2);//20/2^2=20/4=5 System.out.println(20>>3);//20/2^3=20/8=2 } }


Output:
2 5 2
Java Shift Operator Example: >> vs >>>
class OperatorExample { public static void main(String args[]) { //For positive number, >> and >>> works same System.out.println(20>>2); System.out.println(20>>>2); //For negative number, >>> changes parity bit (MSB) to 0 System.out.println(-20>>2); System.out.println(-20>>>2); } }


Output:
5 5 -5
Comparison(Relational) operators
We have six relational operators in Java: ==, !=, >, <, >=, <= == returns true if both the left side and right side are equal != returns true if left side is not equal to the right side of operator. > returns true if left side is greater than right. < returns true if left side is less than right side. >= returns true if left side is greater than or equal to right side. <= returns true if left side is less than or equal to right side.
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
Bitwise Operator

There are six bitwise Operators: &, |, ^, ~, <<, >>

num1 = 11; /* equal to 00001011*/ num2 = 22; /* equal to 00010110 */

Bitwise operator performs bit by bit processing.

num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0. In our case it would return: 2 which is 00000010 because in the binary form of num1 and num2 only second last bits are matching.

num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111

num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0. In our example it would return 29 which is equivalent to 00011101

~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0. In our example it would return -12 which is signed 8 bit equivalent to 11110100

num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to 00101100

Note: In the example below we are providing 2 at the right side of this shift operator that is the reason bits are moving two places to the left side. We can change this number and bits would be moved by the number of bits specified on the right side of the operator. Same applies to the right side operator.

num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010

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
Logical Operator

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.

Logical operators in java are: &&, ||, !

Let’s say we have two boolean variables b1 and b2.

b1&&b2 will return true if both b1 and b2 are true else it would return false.

b1||b2 will return false if both b1 and b2 are false else it would return true.

!b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.

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
Ternary Operator

This operator evaluates a boolean expression and assign the value based on the result.

Syntax:
variable num1 = (expression) ? value if true : value if false

If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.

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



Instagram