Friday, April 22, 2011

JAVA Programming : Operators

JAVA Operators 

A Operator in Java is a Symbol that manipulates one or more primitive data types (arguments) to produce a result. Operators are very important part of Java programming. Below is the list of Operators available in Java.

OPERATORS
Assignment Operators
=








Arithmetic Operators
-
+
*
/
%
++
--








Relational Operators
> 
< 
>=
<=
==
!=









Logical Operators
&&
||
!












Bit wise Operator
&
|
^
>> 
>>> 










Compound Assignment Operators
+=

-=
*=
/=
%=










Conditional Operator
?:







Assignment Operator: It is the most common and simple operator that is used to assign value on its right to the operand on its left.

Arithmetic Operators: As the name suggest these are the operators that are used for performing arithmetic operations. Java provide seven arithmetic operators add, subtract, multiply, divide, mode (returns remainder), unary increment and unary decrement.

Bit Wise Operators: Bit operator in Java are use to manipulate variables at bit level. It is not recommended to use bit wise operatically in normal application programming because in makes code hard to understand. So, we are not going to discusses bit wise operators in details. In code below we shown how to use these operators.

Compound Operators:- Compound operators provide a short way or syntax to write Arithmetic and Bitwise operators. There are eleven compound operators in Java. 

Below is the Java Code that shows how to use Assignment, Arithmetic, Bit wise and Compound Operator in a program. This Class doesn't actually do anything  in terms of producing result, but it shows you the syntax to use these operators. 


public class Operators {
    public static void main(String arg[]) {

      vv//Assignment Operator
      vvint a = 1;     // a has value 1
        int b = 2;    // b has value 2
        int c = a;    // c gets the value of a i.e 1


        //Arithmetic Operators
        a = b + c;    //Addition
        a = b - c;    //subtraction
        a = b * c;    //Multiply
        a = b / c;    //Division
        a = b % c;    //Mode


        a++;      //Increment a by 1
        a--;      //Decrement a by 1

        //Bit Wise Operators
        a = a << 3;   //Left Shift 
        a = a >> 3;    //Right Shift
        a = a >>> 3;  //Unsigned Right Shift

        a = b & c;     //AND
        a = b | c;    //OR
        a = b ^ c;    //XOR

        a = a + b;  //this is normal + operation
        a += b;    //this is compound operation of above 

        //compound Operators
        a -= b;
        a *= b;
        a /= b;
        a %= b;

        a >>= 3;
        a <<= 3;
        a >>>= 3;

        a &= b;
        a |= b;
        a ^= b;
    }
}


Relational Operator:
Relational Operator in Java are binary operators i.e. they are used to compare two operands. There are six relational operators as shown above. Relational operator in java compare to numaric objects and results in a boolean value.


public class Relational{
   public static void main(String args[]){

     int a = 20, b = 10;

     System.out.println("a > b results in : "+(a > b));
     System.out.println("a < b results in : "+(a < b));
     System.out.println("a >= b 
results in :  "+(a >= b));
     System.out.println("a <= b results in :  "+(a <= b));
     System.out.println("a == b results in :  "+(a == b));
     System.out.println("a != b results in :  "+(a != b));

   }
 }



Logical Operator:
Logical operator evaluates two Java boolean values and gives results in boolean value. There are three logical operator in Java. Below is the code that shows the basics of logical operators.


public class Logical{
   public static void main(String args[]){
     boolean a = true;
     boolean b = false;

     System.out.println("a && b 
results in : (a && b));
     System.out.println("a || b results in : (a || b));
     System.out.println("!a results in : " (!a));
   }
}



Truth table for and or and not operations: Here P and Q are Boolean expressions and T stands for true and F stands for false.
Conditional Operator:
Conditional Operator is a ternary operator in Java which evaluates a expression and returns option1 if result is true else returns option2. Take a Look at the figure below.

public class Conditional {
   public static void main(String args[]){   
     int a=5;
     int b=10;
     int max;

     max= a > b ? a : b ; 
     System.out.println("Max = "+max);
   }
}



No comments:

Post a Comment