Skip to main content

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

*/




Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...