- given,
 
class A{}
class B extends A{}
B b = null;
A a = b;
B b1= (B)a;
The above code fragment doesn’t  cause NullPointerException since we are casting amongst the members of inheritance tree. Hence b1 will have the value “null”.
Similaraly,
given,
int[] i = null;
Cloneable c = i;
i = (int [] ) c;
The above code fragment doesn’t result in runtime error(NullPointerException) since  any array extends Cloneable interface.
- All arrays implement      Cloneable interface and java.io.Serializable interface
 - given,
 
long [] l = {1,2,3,4,5};
int [] i  = {10,20,30};
l[0]= i[0];//legal and correct
l = i; //causes compiler error
l = (long[])i;//causes error
- given,
 
final short s1 = 1;
final char c1 = 2;
short s2 = 3;
char c2 = 4;
byte b1,b2,b3,b4;
remember the following,
b1 = s1;//legal because compiler applies implicit casting since it is final variable.
b2 = c1;//legal because compiler applies implicit casting since it is final variable.
b3 = s2;//illegal because compiler doesn’t apply implicit casting.
b4 = c2;//illegal because compiler doesn’t apply implicit casting.
- Important formula,
 
-x = ~x +1;
and 
~x = -x-1;
- if ,
 
int i1;
int i2;
int i3;
i1 = <<someNumber>>;
i2 = <<someComplexMethodName>>();
i3 = i1<<i2;
if i2 is greater than 31 then “i2&0x1f” is used in its place.
- The assignment      operators are,
 
|       =  |      +=  |      -=  |  
|    *=  |      /=  |      %=  |  
|    <<=  |      >>=  |      >>>=  |  
|    &=  |      ^=  |      |=  |  
Note:
There is no assignment operator like &&=, ||= , ~=.
- given,
 
byte b = 1;
long l = 1000;
b += l;//line 3
the above fragment will not cause compiler error since compiler applies implicit casting before executing the line number 3.the above code fragment will be executed as follows.
·         b += l;
·         b = (byte)(b+l);
·         b = (byte(1+1000);
·         b = someValue;
In general ,
E1 op= E2;
            can be written as E1 = (type)(E1 op E2);
            here type is the datatype of the operand E1 and “op” is the operator applied.
given,
interface inter{}
class B implements inter {}
class sub implements B {}
class Test
{
public static void main(String [] args)
{
            B b = new B();
            I i = b;
            sub s = (sub)b;//line 1
}}
- At line one compiler      error doesn’t occur because we applying explicit casting on members of the      same inheritance tree. Thus at line 1 runtime error and not compiler error      is caused. This is be cause B is superclass of sub and an instance of B      cannot be converted into a type of sub class.
 
class Test2
{
            public static void main(String [] args)
            {
                        B[] b = {new B()};
                        sub[] s = new sub[1];
                        Object obj = b;
                        s = (sub[])obj;//line 1
            }
}
- At line one a runtime      error and not compile time error is caused.
 - given,
 
byte a = 1;
byte b = 2;
byte c = (byte)a+b;// Line 1
line 1 causes compiler error since casting is done only to a and not to sum of 
a and b.As a result the RHS will be int and LHS will be byte which results in compiler error.