OPERATORS


Operator:

            It is a symbol that is used to represent a particular operation.

Operands:

            The value given to the operation.

Based on the number of operands operators has three types:

      1.   Unary operator.
2.      Binary Operator.
3.      Ternary Operator.

        I.            Unary Operator:-

It requires only one operand.

Ex. Increment, Decrement operator, Logical NOT operator.

 

     II.         Binary Operator:-

It requires two operands.

Ex. Arithmetic operator, Relational operator, Logical AND operator, Logical OR operator.

 

   III.            Ternary Operator:-

   It requires three operands.

   Ex. Conditional operator.


 Binary Operator:-

 It requires two operands.

  Ex. Arithmetic operator, Relational operator, Logical AND operator, Logical OR operator                .

      a)       Arithmetic Operators:-

Arithmetic Operators

Operators

Operation

+

Addition

-

Substraction

*

Multiplication

/

Division (Quotent)

%

Modulus (Remainder)

 

       b)      Relational Operators:-

Relational Operators

Operators

Operation

> 

Greater than

> =

Greater than equal to

< 

Less than

< =

Less than equal to

= =

Equal to

! =

Not equal to

    Program

  1. public class Maths
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int a=23
  6. int b=22;
  7. System.out.print(a!=b);
  8. }

  Output:

True                                                                                          

         c)      Logical AND Operator (&&)

·         If both the operands conditions are true then only result will be true.

·         If anyone operand is false the result will be false.

 

Logical AND Operator

Operand 1

Operand 2

Result

T

T

T

T

F

F

F

T

F

F

F

F

 

Program

  1. class Maths
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int a=23
  6. int b=22;
  7. System.out.print(a!=b && a>b);
  8. }

Output:

True                                                                      
                                                                             
                                                                             

 

      d)      Logical OR operator ( | | )

·      If any one condition is true then the result will be true.

Logical OR Operator

Operand 1

Operand 2

Result

T

T

T

T

F

T

F

T

T

F

F

F

 

Program

  1. class Maths
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int a=23
  6. int b=22;
  7. System.out.print(a=b | | a>b);
  8. }

Output:

True                                                                              

 

  •      Logic to check whether the number is even or not, n = 4

 

  •       Logic to check whether the number is odd or not, n = 27




                                                         


Ternary Operator:-

·         It requires three operands.

·         Ex. Conditional operator.

 

a.      Conditional Operator

·         It is an example of ternary operator.

·         In conditional operator we will be having three operands.

·         In operand 1 we write the condition, If the condition is true the operand 2 will get called and if the condition is false the operand 3 will get called.

                                                                                        

Syntax


     Program

  1. class Maths
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int a=23
  6. int b=22;
  7. System.out.print(a < b ? a : b);
  8. }

Output:

22                                                                            

 

1.      //Write a java program to check whether the number is even or odd and print by using conditional operator.   n = 1044

  1. class A
  2. {  

  3. public static void main(String [] args)
  4. {
  5.   
  6. int a=1044
  7. int b=2;

  8. System.out.println(a % b==0 ? “Even” : “Odd”);

  9. }

  10.  

Output:

Even                                                         

----------------xxxxxxxxx-------------------------xxxxxxxx-------------------------xxxxxxxx---------------------------

  1. class A
  2. {  

  3. public static void main(String [] args)
  4. {
  5.   
  6. int a=1044
  7. int b=2;
  8. String x="even";
  9. String y="odd";

  10. System.out.println(a % b==0 ? x : y);

  11. }

  12.  

Output:

Even