Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused about the answer

 
nikunj thakur
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Titanic
{
public static void main(String[] args)
{
Boolean bl = true;
boolean b2 = false;
boolean b3 = true;
if((bl & b2) | (b2 & b3) & b3)
System.out.print("alpha ");

if((bl = false))| (b1 & b3) | (bl | b2))
System.out.print("beta ");

}
}
in the above code on compilation showing the following error exception at runtime because in the 2nd if we are assinging b1 to false but what is given in the kb book is that the result of any assignment is the value of the variable after the assignment, the expression (b1 = true) has a result of true. Hence, the if test should succeeds. then why is error
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's correct the errors first :
  • There's an extra parenthesis here (bl = false))
  • b1 should be renamed bl

  •  
    Deepak Jain
    Ranch Hand
    Posts: 637
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have a syntax error
    if((bl = false) )| (b1 & b3) | (bl | b2))
    There is an addional ) Once you remove this and compile the code will compile correctly and there will be no run time exception and since b1 is assigned false in the above if statement the code will run but will not produce any output.
    And yes b1 should be bl or vice versa , THis was driving me nuts but i finally figured it out b1 and bl look almost same.


    Thanks
    Deepak
    [ December 04, 2007: Message edited by: Deepak Jain ]
     
    nikunj thakur
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There's no parantheses error even if i remove the extra ) their is same error
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic