File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn about boolean & operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Qn about boolean & operator" Watch "Qn about boolean & operator" New topic

Qn about boolean & operator

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Does it work similar to using &&.

I mean is a & b same as a&&b; in case both a, b are boolean

SCJP, blog
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No, in the case of &&, if expression a is false then expression b will not even be evaluated.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
A common use of &&:

if ( myObject != null && myObject.isSomething() ) { ... }

if myObject contains null, you are guaranteed that no attempt will be made to invoke myObject.isSomething()

If you use & instead of && in those circumstances, you'd get a NullPointerException

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: Qn about boolean & operator
jQuery in Action, 3rd edition