Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Saturday, June 17, 2017
Logical and bit wise operators in java
Logical and bit wise operators in java
In java both logical and bitwise operators are used. Logical operator returns the true / false value depends on the condition check and bitwise return the same true / false depends on the condition check but the difference between these operator is that logical operator only work with the boolean value where as bitwise operator work with both boolean and numeral values. How that will we see later in this post.
Logical operator
Logical operator in java only deals with the boolean value that means it will return either true or false depending on the condition check. If we try to use any other data type then it will give compilation error.
There are three types of logical operator i.e
1> Logical AND ( && )
2> Logical OR ( | | )
3> Logical NOT ( ! )
Logical AND
It is represented with the double ampersand ( && ) sign. basically it is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator.
Operand 1 Operand 2 Result
True True True
True False False
False True False
False False False
Conclusion: If any of the operand is False then it will return False.
Now think is there any need of checking if 1st operand is false ?
I think No because it is obvious that it will return false only but if 1st operand is true that means it may true or false so it is mandatory to check 2nd operand as well.
Note :- Compiler does the same that if 1st operand is false then no need to check further it further and hence save some compilation time.
for ex-
if ( false && true && true && true ) // It will check the 1st operand if it is false then its obvious that whole statement will lead to false only so compiler will not check further.
You can have any statement in place of true and false here but which returns either true or false value.
Logical OR
It is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator. It is represented like this ( | | ).
Operand 1 Operand 2 Result
True True True
True False True
False True True
False False False
Conclusion: If any of the operand is True then it will return False.
Now think is there any need of checking if 1st operand is true?
I think No because it is obvious that it will return True only but if 1st operand is false then there may be chance of returning true or false so it will be mandatory for compiler to check 2nd operand as well.
Note :- Compiler does the same thing that if 1st operand is true then no need to check it further and hence save some compilation time.
if ( true | | false | | true | | false ) // It will check the 1st operand if it is true then its obvious that whole statement will lead to true only so compiler will not check further.
Here you can have any statement in place of true and false but it should return either true or false value.
Logical NOT
It is a unary operator and it will always return the result as reverse of its operand. Like if operand is true then it will return false and vice-versa.
Operand 1 Result
True False
False True
Conclusion: If the operand is True then it will return False and vice-versa.
for ex-
if ( ! true ) // return false
if ( ! false ) // return true
Bitwise Operator
It is same as Logical operator but it can operate both with boolean and numeral value. There are four types of bitwise operator i.e
1> Bitwise AND ( & )
2> Bitwise OR ( | )
3> Exclusive OR ( ^ )
4> Bitwise NOT ( ~ )
Why named as bitwise ?
It is because for numeral values it deals bit by bit. How it deals bit by bit we will see later in this post.
Bitwise AND
It is a binary operator and represented with a single ampersand ( & ).It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting .
How Bitwise AND works with the numeral value?
It deals with each bit that means it will result as 0 always and 1 when both the value are 0.Now think in zeros and ones. We all know that zero represent always false and one represent true always.
Here it is also the same story that compiler will compare bit by bit and return the result accordingly.
for ex-
System.out.println( (1 & 2 ) ) ; // It will display Zero as output.
1-> 01
2-> 10
---------
00 ( 0 )
---------
Bitwise OR
It is a binary operator.It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting . It is represented like this ( | ).
How Bitwise OR works with the numeral value?
It is same as Bitwise AND but only difference is it will result as 1 always and 0 when both the value are 0.
System.out.println( (1 | 2 ) ) ; // It will display Three ( 3 ) as output.
1-> 01
2-> 10
---------
11 ( 3 )
---------
Exclusive OR
It is a binary operator and represented with ( ^ ) this symbol. It result false when both the operand are either true or false otherwise true So compiler cannot be sure about result unless it checks 2nd operand.
Operand 1 Operand 2 Result
True True False
True False True
False True True
False False False
Conclusion : It will return False if both the are operand are have same value either true or false otherwise true. It is same in case of bits also if both the bits are same either 1 or 0 then it will return 0 otherwise 1.
Have fun with code guys. keep experimenting!
Available link for download
Saturday, May 20, 2017
Local inner class in Java
Local inner class in Java
Local inner class is a class which is define local to the outer class. So it must be defined within the local scope of a class like method, constructor or initialization block . A class within a class in never treated as the local inner class. And most importantly scope of the inner class will be limited to where it is declared, outside of which it is not valid. As local inner class is the local scope to the outer class then we all know that within a local scope we cannot define any static member. So here also in the local inner class we cannot define any static member.
For the local inner class a .class file is generated in the following format :
< outer_class > $ < int_value > < inner class > .class
where as int_value depends on the number of times inner class with the same name is used.
Important points regarding Local inner class
1. We cannot define any static member in a local inner class as because of local scope.
2. But static final variable can be used because of its constant nature.
3. In the generated class file of local inner class the int value indicate that the local inner class has been used how many times in a single program with the same name.
4. We cannot use the local inner class with the same name in the same method, constructor or initialization block as like we cannot define local variable with the same name within the same block.
5. As we cannot define any static member so it cannot be the starting point of any java program.
6. We cannot create any object of local inner class outside the scope of that class.
Simple example to show the working of the local inner class :
For the local inner class a .class file is generated in the following format :
< outer_class > $ < int_value > < inner class > .class
where as int_value depends on the number of times inner class with the same name is used.
Important points regarding Local inner class
1. We cannot define any static member in a local inner class as because of local scope.
2. But static final variable can be used because of its constant nature.
3. In the generated class file of local inner class the int value indicate that the local inner class has been used how many times in a single program with the same name.
4. We cannot use the local inner class with the same name in the same method, constructor or initialization block as like we cannot define local variable with the same name within the same block.
5. As we cannot define any static member so it cannot be the starting point of any java program.
6. We cannot create any object of local inner class outside the scope of that class.
Simple example to show the working of the local inner class :
public class prog{
int x; // Outer class instance member
prog(){ //Outer class constructor
class B { // Local inner class
int x; // Local inner class instance member
B(){ // Local inner class constructor
System.out.println("Local inner class");
}
}
B b = new B(); // Local inner class object
class C{} // Local inner class
}
public static void main(String[] args) {
prog p = new prog();
}
}Available link for download
Subscribe to:
Posts (Atom)