Operators

1) Unary :

a) ‘+’ : All primitive data types are corresponding classes, the category of which is called wrapper classes. The main purpose of a wrapper class is to convert the primitive data types into an object.
The topmost class (super class) of all classes in Java is java.lang.Object class which has a method called toString() which converts the object into string.

b) “++” / “—“ : Incase of pre-increment the RHS is first incremented and then assigned to LHS where as in post increment it is first assigned then incremented.
int a = 10;
System.out.println(a++);    OUTPUT : 10
System.out.println(++a);    OUTPUT : 11

int a =100;            int a = 10;        a    b
a = a++;            int b = 20        10    20
a = a++;            a = ++b;        21    21
a = a++;            b = a--;            20    21
OUTPUT : 100        b=++a;    21        21    21

a = a++;  In this case since it is same variable the memory location is same. Therefore a = 100
It wont check what is on the right side of the variable, hence ++ is ignored. But b = a++; there are two variables, therefore different memory location. Therefore b = 100 and a = 101

c) ‘~’ Bitwise Inversion Operation : When used ~ all zeroes are changed to ones advice versa
int a = 10;            00000000000000000000000000001010
int b = ~a;            11111111111111111111111111110101
System.out.println(b);
OUTPUT : -11

d) ! Boolean compliment operation :
e) cast ( ) :

2) Aritmetic Operator :
    BODMAS - / * + - and %

3) Comparison Operators: All comparative operators return boolean type value.
                            a) Object Type
                            b) Memory Type

a) Object Type : The instance of keyword is used to check whether a instance on LHS belongs to the class on the RHS or not.
This returns a value only when there is a relationship between the instance and a class check.

class A
{
}
class B extends A
{
}
class C extends A
{
}
class InstanceText
{
    public static void main(String[] args)
    {
        A x = new A();
        B y = new B();
        C z = new C();
        System.out.println(x instanceof B);
        System.out.println(y instanceof A);
        System.out.println(z instanceof B);
    }
}
Subclass knows everything about super class. In java multiple inheritance is not allowed.
OUTPUT : compiler error – inconvertible type - System.out.println(z instanceof B);

b) Memory Type : String is the only class in Java that can be created without the new keyword. The two types of memory comparison operators are :
a) Based on location
b) Based on contents

The two types of memory locations are Stack and Heap.
Primitive data types are stored in Stack where as Objects are stored in Heap.
The “= =” operator checks for the memory location of its operand.
The equals() method in the Object class also acts like the “= =” operator but it is overridden in String and Wrapper classes to check for the contents.

class MemoryText
{
    public static void main(String args[])
    {
        String s = "Hello";
        String s1 = "Hello";
        String s2 = new String("Hello");
        String s3 = new String("Hello");
        MemoryText x = new MemoryText();
        MemoryText y = new MemoryText();
        int i = 10;
        int j = 10;
        Integer a = new Integer(i);                //True
        Integer b = new Integer(j);                //False
        System.out.println(s == s1 );                //False
        System.out.println(s2 == s3);                //True
        System.out.println(x == y);                //False
        System.out.println(s2.equals(s3));            //True
        System.out.println(x.equals(y));            //False
        System.out.println(i == j);                //True
        System.out.println(a.equals(b));            //True
        System.out.println(a == b);                //False
    }
}


4) Bitwise :   
                &    |      ^
                1    0    0    1
                1    0    1    0
                -    -    -    -
                1    0    1    1
Others      0    1    0    0

class BitwiseOperator
{
public static void main(String args[])
    {
        int a = 10;
        int b = 20;
        int c = a & b;
        int d = a | b;
        int e = a ^ b;
        System.out.println(c);        // 0
        System.out.println(d);        // 30
        System.out.println(e);        // 30
}
}

5) Short Circuit Operator : && / || If the LHS of “&&” is false the whole answer is false without checking the RHS and if true will act like a normal ‘&’ operator.
class ShortCircuit
{
    public static void main(String args[])
    {
        String s = null;
        if ((s != null) & (s.length() > 5))
        {
            System.out.println("Inside");
        }
        System.out.println("Finished");
    }
}
OUTPUT : Runtime Error – s.length() > 5 – NullPointerException

class ShortCircuit1
{
    public static void main(String args[])
    {
        String s = null;
        if ((s != null) && (s.length() > 5))
        {
            System.out.println("Inside");
        }
        System.out.println("Finished");
    }
}
OUTPUT : Finished

The two ways to convert a String into a primitive int is :
A)    use the static method of Integer class called parseInt()
B)    use the non static method of the Integer class called intValue()

class TernaryText
{
int max(int i, int j)
    {
        int c = (i > j)? i: j;
        return c;
    }
    public static void main(String args[])
    {
        String s = args[0];
        String s1 = args[1];
        int p1 = Integer.parseInt(s);
        Integer i = new Integer(s1);
        int p2 = i.intValue();
        TernaryText E = new TernaryText();
        int r = E.max(p1, p2);
        System.out.println(r );
    }
}

                                    Flow Controls
                                        |
        |------------------------------------------------ |
        |                                |                                        |
    loops                            selection                        jump
        for                                if – else                        break
        do – while                        if                                continue
        while                            switch


switch :     switch is used when
a)    Branching is to balance based on a non boolean value
b)    Multiple conditions have same output

class SwitchTest
{
    public static void main(String args[])
    {
        for(int i=0;i<3;i++)
        {
            switch(i)
            {
                case 2:
                        {
                            System.out.println("Two");
                        }
                default:
                        {
                            System.out.println("Default");
                        }
                case 1:
                        {
                            System.out.println("One");
                        }
            }
        }
    }
}
In a switch case if break is not there between cases it will go in all the cases below a case which is matched. Hence OUTPUT is :
One
One
Two
Default
One

Break: break should always be in a conditional black. You can use break in following :
Break
    loops
switch
labeled breaks

Label Break : In case of a labeled break the break should be followed by the labeled name.
class LabelBreak
{
    public static void main(String args[])
    {
        One:
        {
            Two:
            {
                Three:
                {
                    System.out.println("Before");
                    if(true)
                    {
                        break Two;
                    }
                    System.out.println("After");
                }
                System.out.println("After3");
            }
            System.out.println("After2");
        }
        System.out.println("After1");
    }
}
OUTPUT :    Before
        After2
        After1


Loop Break : To throw the control out of loop.
class BreakFor
{
    public static void main(String[] args)
    {
        for (int i = 1; i < 10 ; i++ )
        {
            System.out.println(i);
            if(i = = 4)
            {
                break;
            }
        }
       
    }
}
OUTPUT :     1        2        3

Continue : Continue is used to manually go down to next alteration without reaching the end of the current black.
class ContinueBreak
{
    public static void main(String args[])
    {
        for(int i=0; i <10 ; i++)
        {
            if(i % 2 == 0)
            {
                continue;
            }
            System.out.println(i);
        }
    }
}
OUTPUT :     1    3    5    7    9