Monday, February 23, 2009

Logical Operators

Logical Operators

Java specify six "logical" operators (&, |, ^, !, &&, and ||).

Bitwise Operators
Okay, this is going to be confusing. Of the six logical operators listed above, three of
them (&, |, and ^) can also be used as "bitwise" operators. Here
are several legal statements that use bitwise operators:
byte b1 = 6 & 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3);
Bitwise operators compare two variables bit by bit, and return a variable
whose bits have been set based on whether the two variables being compared had
respective bits that were either both "on" (&), one or the other "on" (|), or exactly
one "on" (^). By the way, when we run the preceding code, we get
0 15 1

Short-Circuit Logical Operators
The most commonly used of the
five are the two short-circuit logical operators. They are
1. && short-circuit AND
2. || short-circuit OR
They are used to link little boolean expressions together to form bigger boolean
expressions. The && and || operators evaluate only boolean values. For an AND
(&&) expression to be true, both operands must be true—for example,
if ((1 < a=" true" a = " + a); } } When we run the preceding code, we get %java Logic boolean a = false The || operator is similar to the && operator, except that it evaluates to true if EITHER of the operands is true. If the first operand in an OR operation is true, the result will be true, so the short-circuit || doesn't waste time looking at the right side of the equation. If the first operand is false, however, the short-circuit || has to evaluate the second operand to see if the result of the OR operation will be class Or { public static void main(String[] args) { if ((3<7) || (7<8){ System.out.println(">8)) {
System.out.println("Result is true");
}
}
}

What is the result?

Result is true
Result is true

The || and && operators work only with boolean operands.
if (5 && 6) { }
It looks as though we're trying to do a bitwise AND on the bits
representing the integers 5 and 6, but the code won't even compile.

Logical Operators (Not Short-Circuit)
There are two non-short-circuit logical operators.
1. & non-short-circuit AND
2. | non-short-circuit OR
These operators are used in logical expressions just like the && and || operators
are used, but because they aren't the short-circuit operators, they evaluate both sides
of the expression, always! They're inefficient. For example, even if the first operand
(left side) in an & expression is false, the second operand will still be evaluated—
even though it's now impossible for the result to be true! And the | is just as
inefficient: if the first operand is true, the JVM still plows ahead and evaluates the
second operand even when it knows the expression will be true regardless.

You'll have to know exactly which operands are
evaluated and which are not, since the result will vary depending on whether the
second operand in the expression is evaluated:

int z = 5;
if(++z > 5 || ++z > 6) z++; // z = 7 after this code
versus:
int z = 5;
if(++z > 5 | ++z > 6) z++; // z = 8 after this code

Logical Operators ^ and !

1. ^ exclusive-OR (XOR)
2. ! boolean invert
The ^ (exclusive-OR) operator evaluates only boolean values. The ^ operator
is related to the non-short-circuit operators we just reviewed, in that it always
evaluates both the left and right operands in an expression. For an exclusive-OR (^)
expression to be true, EXACTLY one operand must be true—for example,
System.out.println("xor " + ((2<3)>3)));
produces the output: xor false
The preceding expression evaluates to false because BOTH operand one (2 <> 3) evaluate to true.
The ! (boolean invert) operator returns the opposite of a boolean's current value:
if(!(7 == 5)) { System.out.println("not equal"); }
can be read "if it's not true that 7 == 5," and the statement produces this output:
not equal
Here's another example using booleans:
boolean t = true;
boolean f = false;
System.out.println("! " + (t & !f) + " " + f);
produces the output:
! true false
In the preceding example, notice that the & test succeeded (printing true), and
that the value of the boolean variable f did not change, so it printed false.

Useful Links :-

Assignment Operators
Arithmetic Operators
Relational Operators
Conditional Operators
Bitwise Operator Intro
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator

Sponsored Links :-

Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP