Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help required

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic