Pages

Tuesday, 26 April 2022

Java Operators

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

There are many types of operators in Java which are given below:
  1. Unary Operator
  2. Arithmetic Operator
  3. Shift Operator
  4. Relational Operator
  5. Bit wise Operator
  6. Logical Operator
  7. Ternary / Conditional Operator
  8. Assignment Operator
  9. Special Operator
Java Operator Precedence:

1. Unary Operators:
Java Unary Operator Example 1: ++ and --

public 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

Java Unary Operator Example 2: ++ and --
public 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 3: ~ and !
public 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

2. Arithmetic Operators {(5) : +, - ,* ,/ ,% }
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

Java Arithmetic Operator Example
public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
System.out.println(a+b);//15  
System.out.println(a-b);//5  
System.out.println(a*b);//50  
System.out.println(a/b);//2  
System.out.println(a%b);//0  
}
}  
Output:
15
5
50
2
0

Java Arithmetic Operator Example: Expression
public class OperatorExample
{  
   public static void main(String args[])
   {  
     System.out.println(10*10/5+3-1*4/2);  
   }
}  
Output:
21

3. Shift Operators:{(3) : >> , << , >>>}
Java Left Shift Operator:
The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.
Java Left Shift Operator Example
public class OperatorExample
{  
public static void main(String args[])
{  
System.out.println(10<<2);//10*2^2=10*4=40  
System.out.println(10<<3);//10*2^3=10*8=80  
System.out.println(20<<2);//20*2^2=20*4=80  
System.out.println(15<<4);//15*2^4=15*16=240  
}
}  
Output:
40
80
80
240

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

Java Right Shift Operator Example
public 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 >>>
public 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
1073741819

4.Relational Operator{(6) : <,>,<=,>=,==,!=}

Java Relational Operator Example:
public class RelationalOperators
{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) ); 
System.out.println("a != b = " + (a != b) ); 
System.out.println("a > b = " + (a > b) ); 
System.out.println("a < b = " + (a < b) ); 
System.out.println("b >= a = " + (b >= a) ); 
System.out.println("b <= a = " + (b <= a) );
}
}

Output
a == b = false 
a != b = true 
a > b = false 
a < b = true
b >= a = true 
b <= a = false


5.Bit wise Operator{(7) : &,|,~,^,>>,<<,>>>}
1.Bitwise AND (&) represented as & operator in C takes two numbers as operands and does AND gate function on every bit of two numbers. The result of AND is 1 only if both bits are 1.
For example:
  a  = 14 --------------1110     
  b  = 9    -------------1001
(Result a&b : 8) ---1000

2.Bitwise inclusive OR (|) represented as | operator in C takes two numbers as operands and does OR gate function on every bit of two numbers. The result of OR is 1 only if any one of the bits are 1.
For example:
  a  = 14  -------------1110     
  b  = 9    -------------1001
(Result a|b: 15)  ---1111

3.Bitwise exclusive OR ( ^) represented as ^ operator in C takes two numbers as operands and does x-OR gate function on every bit of two numbers. The result of OR is 1 only if any one of the bits are 1.
For example:
  a  = 14  -------------1110     
  b  = 9    -------------1001
(Result a^b: 7)  ---0111

4.Bitwise NOT(~) represented as ~ operator in C takes one number as operands and does NOT gate function  on every bit.
For example:
  a  = 14  -------------1110     
(Result ~a: -15)  ---1's complement is  0001 
To this result perform 2's complement i.e negative number 
  1110
+       1
---------
  1111 which is -15

For example:
  a  = 9  -------------1001     
(Result ~a: -10)  ---1's complement is  0110 
To this result perform 2's complement i.e negative number 
  1001
+       1
---------
   1010 which is -10

5.Bitwise right shift >> represented as >> operator in C takes one number as operands and does right shift one bit function  on every bit.
For example:
  a  = 9      -------------1001     
(Result a>>2: 2)  ---  0010

6.Bitwise left shift << represented as << operator in C takes one number as operands and does left shift one bit function  on every bit.
For example:
  a  = 9   --------------------1001     
(Result a<<2: 36)  --- 100100


Java Bitwise Operator Example:
public class BitwiseOperator
{
public static void main(String args[])
{
int p = 60; /* 60 = 0011 1100 */
int q = 13; /* 13 = 0000 1101 */ 
int c = 0;
c = p & q; /* 12 = 0000 1100 */ 
System.out.println("p & q = " + c );
c = p | q; /* 61 = 0011 1101 */ 
System.out.println("p | q = " + c );
c = p ^ q; /* 49 = 0011 0001 */ 
System.out.println("p ^ q = " + c );
c = ~p; /*-61 = 1100 0011 */ 
System.out.println("~p = " + c );
c = p << 2; /* 240 = 1111 0000 */
System.out.println("p << 2 = " + c );
c = p >> 2; /* 15 = 1111 */ 
System.out.println("p >> 2 = " + c );
c = p >>> 2; /* 15 = 0000 1111 */
System.out.println("p >>> 2 = " + c );
}
}

Output
p & q = 12 
p | q = 61 
p ^ q = 49
~p = -61
p << 2 = 240
p >> 2 = 15
p >>> 2 = 15


6.Logical Operator{(3) : &&,||,|}
Logical Operators are used to combine two or more conditions/constraints. The result of the operation of a logical operator is a Boolean value either true or false.

For example, the logical AND represented as ‘&&’ operator in C returns true when both the conditions under consideration are satisfied. Otherwise it returns false. 
Therefore, a && b returns true when both a and b are true (i.e. non-zero)

a

b

a &&b

0

0

0

0

1

0

1

0

0

1

1

1


For example, the logical OR represented as ‘||’ operator in C returns false when both the conditions under consideration are not satisfied. Otherwise it returns true. 
Therefore, a || b returns false when both a and b are false (i.e. zero)

a

b

a ||b

0

0

0

0

1

1

1

0

1

1

1

1

For example, the logical NOT represented as ‘|’ operator in C returns true for the given false condition similarly it gives false for the given true condition  
Therefore, if a is  true then the logical output  of a is false, similarly if a is false then the logical output is true

a

!a

0

1

1

0


Java Logical Operator Example: 
public class LogicalOperators
{
public static void main(String args[])
{
boolean p = true; 
boolean q = false;
System.out.println("p && q = " + (p&&q)); 
System.out.println("p || q = " + (p||q) ); 
System.out.println("!(p && q) = " + !(p && q));
}
}

Output
p && q = false 
p || q = true
!(p && q) = true

Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn't check the second condition if the first condition is false. It checks the second condition only if the first one is true.

The bitwise & operator always checks both conditions whether first condition is true or false.

public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a<c);//false && true = false  
System.out.println(a<b&a<c);//false & true = false  
}
}  
Output:
false
false

Java AND Operator Example: Logical && vs Bitwise &
public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a++<c);//false && true = false  
System.out.println(a);//10 because second condition is not checked  
System.out.println(a<b&a++<c);//false && true = false  
System.out.println(a);//11 because second condition is checked  
}
}  
Output:
false
10
false
11

Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is true. It checks the second condition only if the first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.

public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a>b||a<c);//true || true = true  
System.out.println(a>b|a<c);//true | true = true  
//|| vs |  
System.out.println(a>b||a++<c);//true || true = true  
System.out.println(a);//10 because second condition is not checked  
System.out.println(a>b|a++<c);//true | true = true  
System.out.println(a);//11 because second condition is checked  
}
}  
Output:
true
true
true
10
true
11

7.Ternary / Conditional Operator
Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.
Java Ternary Operator Example
public class OperatorExample
{  
public static void main(String args[])
{  
int a=2;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}
}  
Output:
2

Another Example:
public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}
}  
Output:
5

8.Assignment Operator{(10): +=,-=,*=,/=,%=,>>=,<<=,&=,!=,^=}
Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.


Java Assignment Operator Example
public class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=20;  
a+=4;//a=a+4 (a=10+4)  
b-=4;//b=b-4 (b=20-4)  
System.out.println(a);  
System.out.println(b);  
}
}  
Output:
14
16

Java Assignment Operator Example
public class OperatorExample
{  
public static void main(String[] args)
{  
int a=10;  
a+=3;//10+3  
System.out.println(a);  
a-=4;//13-4  
System.out.println(a);  
a*=2;//9*2  
System.out.println(a);  
a/=2;//18/2  
System.out.println(a);  
}
}  
Output:
13
9
18
9

Example
public class AssignmentOperator 
{
   public static void main(String args[])
 {
      int a = 5;
      int b = 10;
      int c = 0;
      c = a + b;
      System.out.println("c = a + b = " + c );

      c += a ;
      System.out.println("c += a  = " + c );

      c -= a ;
      System.out.println("c -= a = " + c );

      c *= a ;
      System.out.println("c *= a = " + c );

      a = 10;
      c = 15;
      c /= a ;
      System.out.println("c /= a = " + c );

      a = 10;
      c = 15;
      c %= a ;
      System.out.println("c %= a  = " + c );

      c &= a ;
      System.out.println("c &= a  = " + c );

      c ^= a ;
      System.out.println("c ^= a   = " + c );

      c |= a ;
      System.out.println("c |= a   = " + c );
      c <<= 2 ;
      System.out.println("c <<= 2 = " + c );

      c >>= 2 ;
      System.out.println("c >>= 2 = " + c );

      c >>= 2 ;
      System.out.println("c >>= 2 = " + c );

   }
}

Output
c = a + b = 15
c += a  = 20
c -= a = 15
c *= a = 75
c /= a = 1
c %= a  = 5
c &= a  = 0
c ^= a   = 10
c |= a   = 10
c <<= 2 = 40
c >>= 2 = 10
c >>= 2 = 2

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

After using the type cast we can remove the compile time error:
public class OperatorExample
{  
public static void main(String args[])
{  
short a=10;  
short b=10;  
a=(short)(a+b);//20 which is int now converted to short  
System.out.println(a);  
}
}  
Output:
20

9.Special Operator
instanceof Operator
The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface) i.e for object reference variables.
Syntax:
( Object reference variable ) instanceof (class/interface type)

Java instanceof Operator Example:
public class InstanceOfOperator
{
public static void main(String args[])
{
String location = "mvpcolony";
// following will return true since location is type of String
boolean result = location instanceof String; 
System.out.println( result );
}
}
Output :
true

Java Operator Precedence and Associativity

Example :
class Precedence 
{
    public static void main(String[] args) 
{
      int a = 10, b = 5, c = 1, result;
    result = a - ++c - ++b; // 10 - 2 - 6  
    System.out.println(result);
    }
}
Output:
2

Example :
class Precedence 
{
    public static void main(String[] args) 
{
      int a = 10, b = 5, c = 1, result;
     result = a * ++c - ++b; // 10 * 2 - 6  
     System.out.println(result);
    }
}
Output:
14

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...