aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Question " Watch "SCJP Question " New topic
Author

SCJP Question

Kanan Jariwala
Greenhorn

Joined: Jun 22, 2000
Posts: 17
int i = 10;
int j = 10;
boolean b = false;
if(b= i == j)
System.out.println("True");
else
System.out.println("False");

This prints "True"
But if() is suppose to contain only bollean expressions,
how does this work?
Explain!!
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hello kanan,
the explanation to this output goes like this.
if(b= i == j)
remember
1) during assignment operation (i.e. '=' operation )first of all the RHS expression is calculated and the result is assigned to LHS.
so in ur case RHS expression is
i==j
which is true so now the if(...) becomes
if(b = true)
now true will be assigned to b
so finally if(...) will be considered
if(true)
so the expression inside if() has value true so the output is true.
hope this explain things.
do post any other doubt of urs.
regards
deekasha


Gopinath Rajgopal
Ranch Hand

Joined: Jun 26, 2000
Posts: 31
Original Code:

int i = 10;
int j = 10;
boolean b = false;
if(b= i == j)
System.out.println("True");
else
System.out.println("False");
To make the if statement more clear, look at it this way:
if(b = (i==j))
Here, i==j returns true, which is assigned to b. Now, your if statement has its required boolean condition - b - which is true. Hence the result.
Kanan Jariwala
Greenhorn

Joined: Jun 22, 2000
Posts: 17
thanks deeksha and gopinath, u'll explained that one fine
 
wood burning stoves
 
subject: SCJP Question