JAVA OPERATORS

JAVA OPERATORS

 

An operator is a symbol that are used in program to compute values and test logical expressions .

Java operators can be classified into different categories:-

  • Arithmetic operator
  • Assignment operator
  • Comparison operator
  • Unary operator
  • Logical operator
  • Shift operator
  • Bitwise operator
  • Conditional operator
  • Instance of and members selection operator
  • New operator

 

Arithmetic operator :- Arithmetic operator is used to perform the arithmetic operations like Addition , Subtraction , Multiplication ,Division and Modulo Division.

 

These can be expressed as:-

                              

Operator

Operator name

          +

Addition

          -

Subtraction

          *

Multiplication

          /

Division

         %

Modulo

               

Assignment operator:- Assignment operator are used to assign the value of expression to the left hand operator .

It has two types -:

  • Simple assignment operator
  • Short hand operator

Simple assignment operator:- This is equal to (=) symbol is called assignment operator.

               Syntax: variable = expression

Short hand operator:- Consider the example x=x+3 ,In java we can also write this type of statement as x+=3 , this also perform the same operation

Operator

Example

      +=

A+=B

      -+

A-=B

      *=

A*=B

      /=   

A/=B

      %=

A%=B

 

Comparison operator:- Logical expression are developed using  comparison operators or relational operators. The operators are evaluating true or false . It is mostly used in conditional statements and looping structure.

Operator

Operator name

       <

Less than

       <=

Less than or Equal to

      >=

Greater than or equal to

      ==

Equal to

      !=

Not equal

                              

Unary operator:- Java supports two type of operator

  • Increment operator
  • Decrement operator

 

  • Increment operator: The ++ increment operator add 1 to the operand . It is available in two forms .
    • Prefix increment              ++m
    • Postfix increment             m++
  • Decrement operator: The decrement operator – can also be used in both prefix and postfix forms It decreases the value of variable by one.
  • Prefix decrement              --m
  • Postfix decrement            m—

Shift operator:- A shift operator allows you to perform bit operator allows you to perform bit manipulation on data . Java supports three types of Shift operator

  1. Left shift    <<
  2. Right shift  >>
  3. Right shift with zero fill >>> or unsigned right shift

Bitwise operator:- Java supports four type of bitwise operator they are AND , OR , XOR , and NOT

  • Bitwise AND :- The bitwise AND is represented by a ‘&’ symbol AND result in 1 if both the bits are 1 any other combination results is 0

 

Operand1     

Operand2

Result

        0

          0

        0

        1  

          0

        0

        0

          1

        0

        1

          1    

        1   

 

  • Bitwise OR :- The bitwise operator is represented by the symbol  ‘||’.

 

Operand1     

Operand2

Result

        0

          0

        0

        1  

          0

        1

        0

          1

        1

        1

          1    

        1   

 

  • Bitwise XOR :- The bitwise XOR operator is represented  by the symbol ’^’ .If the two operand is different the result is one otherwise the result is zero.

 

Operand1     

Operand2

Result

        0

          0

        0

        1  

          0

        1

        0

          1

        1

        1

          1    

        0  

  • Bitwise not :- NOT operator is used to Invert the value of each bit of the operand .

 

Operand1     

Result

        1  

          0

        0

  1.  

 

Logical operator:- There are two logical operators available in Java .Java uses logical AND operator ‘&&’ and logical OR operator as ‘||’. Use of logical operator is to combine the result of Boolean expression.

Conditional operator or Ternary operator :- Java has a decision operator Called conditional operator. The conditional operator is known as ternary operator .

               It’s general form is –

               Test condition ? expressions:expression2;

Instance of operator:- This operator test whether an instance derive from a particular class or interface ‘Instance of ’ is the reserved word in java

               The return type is Boolean .

New operator :- When we allocate memory to an object we must use the new operator because when we create an instance of the class we must allocate memory  for it .

It is in two ways:

  • Flower rose;
    1. Rose =new flower();
  • Flower rose =new flower();

 

 

Post a Comment

Previous Post Next Post