aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused about  the answer 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 "confused about  the answer" Watch "confused about  the answer" New topic
Author

confused about the answer

nikunj thakur
Ranch Hand

Joined: Sep 23, 2007
Posts: 47
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Let's correct the errors first :
  • There's an extra parenthesis here (bl = false))
  • b1 should be renamed bl


  • [My Blog]
    All roads lead to JavaRanch
    Deepak Jain
    Ranch Hand

    Joined: Aug 05, 2006
    Posts: 637
    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 ]

    SCJP, SCWCD, SCBCD
    nikunj thakur
    Ranch Hand

    Joined: Sep 23, 2007
    Posts: 47
    There's no parantheses error even if i remove the extra ) their is same error
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: confused about the answer