Operators & Assignment part 3

Operators & Assignment part 3

                                                                     Operators & Assignment

1-:instanceof Operators

2-:Bitwise Operators





                                                                       instanceof Operators

we can use instanceof operator to check whether the given object is of a particular type are not.

Example-:

/*

list is a collection of objects.

List l=new List();

l.add(Customer);

l.add(Student);

l.add(Test);

Object o=l.get(0);

if(o instanceof Student){

     Student stu=(Student)o;

}else if(o instanceof Customer){

      Customer cust=(Customer)o;

}

*/

Syntax-:

    r instanceof x          (r is object reference x is class or interface name)

/*



Thread t =new Thread();

System.out.print(t instanceof t);//true

System.out.print(t instanceof Object);//true

System.out.print(t instanceof Runnable);//true

*/


To use instanceof operator compulsory there should be some relation b/w argument types(either child to parent or parent to child or same time) otherwise we will get compile time error.

the incompatible type found java.lang.Thread but required java.lang.String.

Example 2-:

/*

   Thread t=new Thread();

   System.out.print(t instanceof String);//compile time error the incompatible type found java.lang.Thread but required java.lang.String.


Note-: for any class or interface x null instanceof x is always false.

/*

System.out.print(null instanceof t);//false

System.out.print(null instanceof Object);//false

System.out.print(null instanceof Runnable)//false

/*


                                                        Bitwise Operators

^ (XOR) -: Return true iff both argument are different.

& (AND)-: Return true if both argument are true.

| (OR) -:Return true iff at least one argument is true.


/*

System.out.print(true & true)//true

System.out.print(true & false)//false

System.out.print(true | true)//true

System.out.print(false | true)//true

System.out.print(true ^ true)//false

System.out.print(false ^ true)//true

*/

we can apply these operators for integral typs also

/*

/*

      100

 &  101

_______

      100

*/      

System.out.print(4&5);//4

/*

      100

 |    101

_______

      101

*/  

System.out.print(4|5);//5

/*

      100

 ^   101

_______

      001

*/  

System.out.print(4^5)//1

*/



                                                       Bitwise of complement operator(~)

operator tild(~) can't be applied for boolean type.


we can apply this operator only for  integral type but not for boolean type if trying to apply for boolean type then we will get compile time error.

/*

 System.out.print(~true)//compile time error

/*

 4= 000000000000000000000000000000100

~4=11111111111111111111111111111011

2's complement

       MSB

       100000000000000000000000000000100

1's complement

 ~4=100000000000000000000000000000100

                                                                     +1

______________________________________

        100000000000000000000000000000101

 MSB is 1 which indicates the negative symbol so and is -5

*/

System.out.print(~4)//-5

*/

Note-:MSB(most significant bit) access sign bit  o means positive number 1 means negative 

positive number represent directly memory where as negative number indirectly memory in 2's complements form.


                                                    Boolean complement operator (!)

we can apply this operator only for boolean types but not for integral types.

/*

System.out.print(!3)//operator ! cannot be applied to int

System.out.print(!false)//true

*/




Previous Post Next Post

Most Recent