File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help required" Watch "help required" New topic
Author

help required

VINCE CARTER
Greenhorn

Joined: Sep 26, 2004
Posts: 13
Which of the following will not compile without error?
A:
boolean b1=true;
boolean b2=true;
System.out.println(b1|b2);
B:
boolean b1=true;
boolean b2=true;
System.out.println(b1||b2);
Why the result is B not A ??
Thanks!!!
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
b1|b2 = bitwise manipulation
b1||b2 = boolean logic read b1 or b2.

Let's use a simple example for bitwise manipulation

int a = 1, b = 2, c;

c = a | b;

binary value a 0001 = 1
binary value b 0010 = 2
---------------------------
binary value c 0011 = 3

Rules for bit wise or are if on of the bits is 1, the result is 1.

Hope this helps.
-C


SCJP 1.4 SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Both of these examples compile, and when run result in true.

The one involving the operator is the "non-shortcutting" logical OR operator when its operands are boolean. (It is also a bitwise or operator when its operands are integer types).

The second operator || is the "shortcutting" logical OR operator which takes only boolean operands.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
Consider Paul's rocket mass heater.
 
subject: help required