Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

different between I and || operator

Thread view: 
Milad - 09 Jan 2008 23:00 GMT
what is the different between || and | operator or & and &&
all of the have the same value. see:
false || true = true
true || flase = true
false && true = false
true && false = false
Lord Zoltar - 09 Jan 2008 23:21 GMT
> what is the different between || and | operator or & and &&
> all of the have the same value. see:
> false || true = true
> true || flase = true
> false && true = false
> true && false = false

Here's a difference:
true && false == false
true || false == true

&& and || are logic operators. Research "Boolean logic" or "Boolean
Algebra". && is pronounced "AND", || is pronounced "OR".

& and | are bitwise operators. They perform bitwise "AND" and "OR"
operators.
SEE: http://www.leepoint.net/notes-java/data/expressions/bitops.html
Or search for "Java Bitwise Operators" (the link above is the first
google result I got)
Patricia Shanahan - 09 Jan 2008 23:25 GMT
...
> & and | are bitwise operators. They perform bitwise "AND" and "OR"
> operators.

& and | are only bitwise operators if the operands are integers. For
boolean operands, they are the same as && and || except for forcing
right hand side evaluation, regardless of the left hand side value.

Patricia
Lord Zoltar - 09 Jan 2008 23:37 GMT
> ...
>
[quoted text clipped - 6 lines]
>
> Patricia

Oh yeah, and these operators are all commutative, so your examples:
false || true = true
true || flase = true
false && true = false
true && false = false
were a bit redundant because:
false || true == true || false
and
true && false == false && true
Lasse Reichstein Nielsen - 09 Jan 2008 23:45 GMT
> Oh yeah, and these operators are all commutative, so your examples:
> false || true = true

The short-circuit operators (|| and &&) are not computationally commutative.
If both sub-expressions evaluate to a value, then they are, but if one
aborts abruptly, the order matters.

Consider haveing the method
  boolean foo() { throw new IllegalStateException(); }
then "foo() && false" and "false && foo()" does not give the same
result. The former throws an exception, and the latter evaluates
to true.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Lew - 10 Jan 2008 00:12 GMT
> Consider haveing the method
>    boolean foo() { throw new IllegalStateException(); }
> then "foo() && false" and "false && foo()" does not give the same
> result. The former throws an exception, and the latter evaluates
> to true.

s/false/true/g

Signature

Lew

Lasse Reichstein Nielsen - 10 Jan 2008 06:07 GMT
>> Consider haveing the method
>>    boolean foo() { throw new IllegalStateException(); }
[quoted text clipped - 3 lines]
>
> s/false/true/g

Ooops. Yes, ofcourse it should be "... evaluates to false".
/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen - 09 Jan 2008 23:22 GMT
> what is the different between || and | operator or & and &&
> all of the have the same value. see:
> false || true = true
> true || flase = true
> false && true = false
> true && false = false

The "||" and "&&" operators are "short-circuit" operators. If
they know the result after evaluating the first sub-expressions,
they never even look at the second (that is, if the first expression
is false for "&&" or true for "||").

This allows you to write things like:

 boolean isValid = (bar != null) && bar.isValid();

If the "bar" variable holds null, the "bar.isValid()" is never
evaluated.
If instead you write:

 boolean isValid = (bar != null) & bar.isValid();

and "bar" is null, then this gives a NullPointerException.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Roedy Green - 10 Jan 2008 00:16 GMT
>what is the different between || and | operator or & and &&
>all of the have the same value. see:
>false || true = true
>true || flase = true
>false && true = false
>true && false = false

see http://mindprod.com/jgloss/oroperator.html
http://mindprod.com/jgloss/andoperator.html
http://mindprod.com/jgloss/mccarthyoroperator.html
http://mindprod.com/jgloss/mccarthyandoperator.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Adam Maass - 10 Jan 2008 06:50 GMT
> what is the different between || and | operator or & and &&
> all of the have the same value. see:
> false || true = true
> true || flase = true
> false && true = false
> true && false = false

|| and && avoid evaluating their right-hand argument; | and & always
evaluate their right-hand argument.

false && foo()   -- foo() is never called because the value of the
expression is determinable without calling foo() -- it is false since the
left hand argument is false.

true || foo()  -- foo() is never called because the value of the expression
is determinable without calling foo() -- it is true since the left hand
argument is true.

In contrast,

false & foo()  -- foo() is called.

true | foo() -- foo() is called.

-- Adam Maass


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.