• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Don't understand the result of Question 6, page 311, Chapter 4

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand the result of the followig question from the book SCJP 1.5 from K.S. & B.B (page 310/311, Chapter 4). Could somebody please provide an additional explanation?

class Titanic {
public static void main(String[] args) {
Boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ((b1 & b2) | (b2 & b3) & b3)
System.out.print("alpha");
if ((b1 = false) | (b1 & b3) | (b1 | b2))
System.out.print("beta");
}}

Result: E. No output is produced.
Explanation: In the second if test, the leftmost expression is an assignment, not a comparison. Once b1 has been set to false, the remaining tests are all false.


I feel rather stupid, as the I don't understand the result in spite of the given explanation, but it's better to ask than forget it. Why does the last if test evaluate to false just because b1 is set to false?

Thank you.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(b1 = false) | (b1 & b3) | (b1 | b2)

b1 = false and appears in each part of the expression. So it's equivalent to :
(false) | (false & true) | (false | false) =
false | false | false = false

So nothing is printed.
 
Q Drini
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christian,

Thank you very much for your explanation. I now understand the result.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(b1 == b2) compares two values and evaluates to a boolean true or false depending on whether or not they are equal

(b1 = b2) assigns the value of b2 to b1 and then the evaluation takes on whatever that value was (if b2 was true then the evaluation is true, if b2 was false then the evaluation is false.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic