This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators" Watch "operators" New topic
Author

operators

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
hi
when i compile this code I got compile error.
Here at ////1

Do let me know why ???

public class haiii
{
public static void main (String[] args)
{
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = b2) ////1
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}


If I change the above code at ////1 to below as it compiles fine

i dont understand why ...

public class haiii
{
public static void main (String[] args)
{
boolean b1 = false;
boolean b2 = false;
if (b2 = b1 = b2) ////1
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
manogna edintipal
Ranch Hand

Joined: Aug 16, 2005
Posts: 51
The '!=' operator has higher precedence than '=' operator

so the if condition evaluates to (booleanvalue)=variable.

this leads to the complie time error....


Sanju
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
This is one of those dangerous and easy to overlook assignments which can cause defects which cannot be traced back easily. Be careful please.

Anyway - for the purpose of the SCJP exam.

In your second code fragment the result of the assignment can be assigned to the variable b2.

However in the first example, the result of the variable cannot be assigned and as such the code won't compile.

It would have been legal to write:


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: operators
 
Similar Threads
how to compile java source code with assertion ?
Explain me this Programm
K & B boolean Question
Wrapper Equality test
Boxing conversions question