Java Tutorial


OOPS concept


Collection

Java - Operators

Java provides a rich set of operators environment.


Java Operators can be divided into following categories.

  • Arithmetic Operators
  • Relation Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic Operators

These operators are used in mathematical expressions.

Let's suppose the value of X = 10 and Y = 5

OperatorDescriptionExample
+Add two operandsX+Y gives 15
-Subtract second operand from firstX-Y gives 5
*Multiply two operandsX*Y gives 50
/Divide first operand by secondX/Y gives 2
%Remainder of divisionX%Y gives 0
++Increment operator by 1X++ gives 11
--Decrement operator by 1Y-- gives 4

Relation Operators

OperatorDescriptionExample
==Check if values of operands are equal, if yes then condtion is trueX==Y gives false
!=Check if values of operands are not equal, if yes then condtion is trueX!=Y gives true
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.X>Y gives true
<Checks if the value of left operand is smaller than the value of right operand, if yes then condition becomes true.X<Y gives false
>=Checks if the value of left operand is greater than or equal to value of right operand, if yes then condition becomes true.X>=Y gives false
<=Checks if the value of left operand is smaller than or equal to the value of right operand, if yes then condition becomes true.X<=y gives true

Logical Operators

Suppose X = true and Y = false

OperatorDescriptionExample
&&Logical ANDX&&Y is false
||Logical ORX||Y is true
!Logical NOT!Y is true

Bitwise Operators

Bitwise operator works on bits. Operation is performed bits by bits.
Representation of X(10) in binary : 1010
Representation of Y(5) in binary : 0101

Now when Bitwise AND(&) operation is performed it will give:0000, therefor output will be 0.
When Bitwise OR(&) operation is performed it will give:1111, therefor output will be 15.

OperatorDescriptionExample
&Bitwise ANDX&Y gives 0
|Bitwise ORX|Y gives 15
^Bitwise XORX^Y gives 12
<<Left Shift (shift bits in left by given number)X<<2 gives 640
>>Right Shift (shift bits in right by given number)Y>> gives 2

Assignment Operators

OperatorDescriptionExample
=Assigns values from right side operands to left side operand.Z = X + Y
+=Add right operand to left operand and assigns the value to right operandZ+=X, means Z=Z+X
-=Subtracts right operand from the left operand and assign the result to left operand.Z-=X, means Z=Z-X
*=Multiplies right operand with the left operand and assign the result to left operand.Z*=X, means Z=Z*X
/=Divides left operand by right operand and assign the result to left operand.Z/=X, means Z=Z/X
%=Takes modulus using two operands and assign the result to left operand.Z%=X, means Z=Z%X

Misc Operators


Conditional Operator

Also known as Ternary operator.

Syntax

variable = (expression) ? value1(when true) : value2(when false)

If the expression is true in above syntax then value1 is assigned to variable on the left hand side else value2 will be assigned.

Example

public class Demo{

   public static void main(String args[]) {
    
int x = 5, y=3, max;

      max = (x > y) ? x: y;
      System.out.println( "Greater number is: " + max );
   }
}

Output

Greater number is: 5



instanceOf Operator

Used only for obects. This Operator checks whether a particular object belongs to a class/interface or not.

Example

class Animal{
String name;
}

public class Dog extends Animal{

   public static void main(String args[]) {

      Dog dog = new Animal();
      boolean x =  dog instanceof Animal;
      System.out.println( x );
   }
}

Output

true

Advertisment

Share


Advertisement