Monday, 8 August 2016

Operators

4.       Operators:
Operator is a syntax, it will perform a particular operator over the provided operands.
To prepare java application, java has provided the following list of operators.
1.         Arithmetic operator :
+,-,*,/,%,++,--,…………
2.       Assignment operators :
=,+=,-=,*=, /=,%=
3.       Comparison operators:
==, !=, <, >, <=, =>,……..
4.       Logical Boolean operator :
&, |, ^
5.       Logical Bitwise operator:
&, |,^,<<,>>………………
6.       Ternary operator:
Exper? Exp2: Exp3;
7.       Short-circuit operators :
&&,||
Ex:-
class Test
{
                public static void main(String[] args)
                {
                    int a=10;
                    System.out.println(a);
                    System.out.println(a++);
                    System.out.println(++a);
                    System.out.println(a--);
                    System.out.println(--a);
                    System.out.println(a);
    }
}
Output :

10
10
12
12
10
10

Ex1:-

class Test
{
        public static void main(String[] args)
        {
                        int a=7;
            System.out.println(++a+--a);
           
    }
}
Output :
>>int a=7;
++a + --a
     8+    7
       15

Ex2:-
class Test
{
        public static void main(String[] args)
        {
                        int a=6;
            System.out.println(a++*--a+a--*++a);
           
    }
}
Output:
>>6*6+6*6
>>36+36
>>72


Ex 3:
class Test
{
        public static void main(String[] args)
        {
                        int a=5;
            System.out.println((++a-a--)+(--a+a++)*(++a-++a)+(--a+--a));
           
    }
}
Output:
>>(6-6)+(4+4)*(6-7)+(6+5)
>>(0)+(8)*(-1)+(11)
>>0-8+11
>>-8+11
>>3

Date :02- august-2016

Boolean operators
Boolean truth table
A
B
A&B
A|B
A^B
T (1)
T (1)
T (1)
T (1)
F (0)
T (1)
F (0)
F (0)
T (1)
T (1)
F (0)
T (1)
F (0)
T (1)
T (1)
F (0)
F (0)
F (0)
F (0)
F (0)

EX:-
class Test
{
        public static void main(String[] args)
        {
                        boolean b1=true;
                        boolean b2=false;
                        System.out.println(b1&b1); //true
                        System.out.println(b1&b2);//false
                        System.out.println(b2&b1);//false
                        System.out.println(b2&b2);//false


                        System.out.println(b1|b1); //true
                        System.out.println(b1|b2);//true
                        System.out.println(b2|b1);//true
                        System.out.println(b2|b2);//false


                        System.out.println(b1^b1); //false
                        System.out.println(b1^b2);//true
                        System.out.println(b2^b1);//true
                        System.out.println(b2^b2);//false
           
    }
}

Bitwise operators
Ex:-
class Test
{
        public static void main(String[] args)
        {
                        int a=10;
                        int b=2;
                        System.out.println(a&b);
                        System.out.println(a|b);
                        System.out.println(a^b);
                        System.out.println(a<<b);
                        System.out.println(a>>b);
    }
}

Output:
2
10
8
40
2
Explanation
>>int a=10;-----à1010
>>int b=2;-------à0010
-----------------------------------
>>a&b-à10&2à0010=>2
------------------------------------
>>a|bà10|2à  1010=>10
------------------------------------
>>a^bà10^2à  1000=>8
------------------------------------
>>a<<bà10<<2àexpression 8 bit operators
                                  00001010à10
                         <<         00101000à40
>>a>>bà10>>2àexpression 8 bit operators
                                   00001010à10
                  > >       00000010 à2     

Date :03- august-2016
                 Short-circuit operators:-
The main intention of logical-OR operator, if the first operator, value is  “true “ then i6 is not required to check second operator value, directly, we can predict the result of the overall “OR” express is “true”.
In case of ‘|’ operator, even through first operand value the only JVM will predict the overall ‘OR’ operator expression, in the case evaluation second operand expression is unnecessary, it will increases application time and it will reduce application performance.     
In case of || operator, if first operator value is “true” then JVM will not check second operand value, directly, JVM will predict the overall ‘OR” expression overall is true. In this case, JVM is not executing second operand value explicitly, it will reduce application execution time and it will improve application performance.
Ex:-
class Test
{
                public static void main(String[] args)
                {
                                int a=10;
                                int b=10;
                                if((a++==10)|(b++==10))
                                {
                                                System.out.println(a);
                                                System.out.println(b);

                                }

                                int c=10;
                                int d=10;
                                if ((c++==10)||(d++==10))
                                {
                                                System.out.println(c);
                                                System.out.println(d);
                                               
                                }
    }
}
Output :
11
11
11
10
& Vs &&
In case of “logical AND” operator, if the first operator value is “false” then it is not required to check second operator value, directly we can get “false” as result from the overall “AND” operation result.
In case of “A” operator, even through first operand value is “false” still JVM will go to second operand value and JVM will get the overall expression result on the basis of first operand and second operator values, in this case evaluation second operand expression is unnecessary, it will increase application execution time and it will reduce application performance.
In case of “&” operator, if first operand value is “false” then JVM will not evaluation second operand value directly, JVM will get the overall AND Expression result that is “false” , here JVM is not evaluating second operand expression unnecessary, it will reduce application execution time and if will improve application performance.
Ex:-
class Test
{
                public static void main(String[] args)
                {
                                int a=10;
                                int b=10;
                                if((a++!=10)&(b++!=10))
                                {

                                }
                               
                                                System.out.println(a);
                                                System.out.println(b);

                                int c=10;
                                int d=10;
                                if ((c++!=10)&&(d++!=10))

                                {

                                }

                System.out.println(c);
                                                System.out.println(d);
                                               
                }
}
Output :-
11
11
11
10



No comments:

Post a Comment