Catch us on

Tuesday, March 29, 2016

operators in java

Hello guys I am back with a new tutorial on java, you can read my previous on either of the link given below:
So let’s start today I will tell you something about operators in java. Java has basically
1.       Relational operator (<,>, ==, != ) : they are used to evaluate a relation between two equations or more. It returns a Boolean value either true or false.
Example:
class Operator{
  public static void main(String[] args)
    {
                 System.out.println(5<4);
                 System.out.println(5>4);
                 System.out.println(5==4);
                 }
                }
On executing this code we will get:
false
true
false


2.       Bitwise operator ( &,|,^,~): they are used to perform operations like AND,OR , XOR NOT respectively. It performs respective operations with the binary code of the operands and returns the result in decimal number system.
Example
 class Operator{
  public static void main(String[] args)
    {
                 System.out.println(5|4);
                 System.out.println(5^4);
                 System.out.println(5&4);
               System.out.println(~4);
                 }
                }
After execution of above code we get the output as
5
1
4
-5
3.       Logical operator (&&,||,!): logical AND, logical OR, logical NOT(it is just used to reverse the result if the result is true then it will make it false and vice versa) are used basically used when we require any logical operations to be performed on a set of operands. They are generally used with “if” and “while” conditions. It also returns Boolean value
Example :
  class Operator{
  public static void main(String[] args){
                  if((5<6)||(2>3)){
                    System.out.println("This statement will run if either of the conditions is true");
                 }
                if((5>4)&&(4<10)){
                    System.out.println("This statement will run if both the conditions are true");                 }
   }
}
Output:
This statement will run if either of the conditions is true
This statement will run only if both the conditions are true
4.       Assignment operator: assignment operator is nothing but jus a equal “=” ssign which is used to assign a value to any variable.
Example: a=5; // this will assign the value to the variable a.
5.       Conditional or ternary operator (? :): this operator works as same as “if-else” statement however it is not in practice because sometime it becomes cumbersome to use this.
Syntax a=4>5?4:5; this means as same as
    if(4>5)
    a=4;
    else
    a=5;
6.       Unary operator: (++,--): this operator just needs only one operand to be operated on. It has the same meaning variable+1 or variable-1.
Example:  a++; or ++a //a=a+1
                  a--; or --a;// a=a-1
Note: however it also becomes little cumbersome job when unary operator (++,--) is used with an assignment operator (=).
x=y++; //first x will be assigned the value of y then y will be incremented by one.
x=++y; // first the value of y will be incremented by one then it will be assigned to x.
Same case goes for –.
7.       Shift operator(<<,>>): it is used to modify the value either by shifting of the operand present at the left of shift operator left or to the right by the number of bits present at the right of the shift operator. The symbols have their respective meanings (<<left shift >> right shift).
Syntax: 5>>1 will give 2 and 5<<1 will give 10.
Note: right shift decrease the value and left shift increase the value.
The whole process of shifting is carried on after converting the decimal number into its equivalent binary.



THANK YOU FOR READING THIS ARTICLE.
PLEASE SHARE IT ON FACEBOOK,TWITTER AND OTHER SOCIAL NETWORKS AND RECOMMEND YOUR FRIENDS TOO.

Kishan Agarwal

Please Share


No comments:

Post a Comment