*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean Que 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 "Boolean Que" Watch "Boolean Que" New topic
Author

Boolean Que

Hemant Gupt
Greenhorn

Joined: May 12, 2008
Posts: 26
Source : Marcus Green

What will happen when you attempt to compile and run this code?

public class MyIf{
public static void main(String argv[]){
MyIf mi = new MyIf();
}
MyIf(){
boolean b = false;
if(b=false){
System.out.println("The value of b is"+b);
}
}
}

1) Run time error, a boolean cannot be appended using the + operator
2) Compile time error the parameter to the if operator must evaluate
to a boolean
3) Compile time error, cannot simultaneously create and assign value for boolean
value
4) Compilation and run with no output



Answer is 4)
I know that compilation will suceed, but why there is no output. As per me,
System.out.println("The value of b is"+b);
should be executed.
Minu Jain
Ranch Hand

Joined: Mar 24, 2008
Posts: 74
This is something you must really pay attention to while preparing for SCJP.
Notice that the if statement is NOT doing any comparison(as it is NOT using ==) but it is doing an assignment instead(as it is using =).
Though there is nothing wrong with such a syntax, but ultimately in your code the condition for if is false. Hence it will not go inside the if block at all.
Hope my explanation is clear enough


SCBCD5, SCWCD5, SCJP5
"Even if you're on the right track, you'll get run over if you just sit there."
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Rancher ,

You should keep in mind two cases .

class{
// some codes
boolean b ;
if(false) {
// some codes
}
if(b=false) {
// some codes
}
// other stuffs .
}
Inside if , assignment to boolean variable is valid .
Both if statements are true in java .Since both expression will result in false , there will not be any output .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Hi

In the if statement, if you use the assignment operator it will first assign the right-hand side value to the left-hand side variable. Then the if statement checks the variable 'b'(in your example). So it can be split up into following steps to make it simple:

if(b=false) is split up into following:

1. b=false;
2. if(b)

In the second step b evaluates to false so it will not enter into the 'if' block and it will enter in the else block if there is one.

Hope it is clear.

Kris
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
Thank you guys I also had a similar doubt but now it is cleared because of your explanation


SCJP
vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
thank you krishnamoorthy i had this doubt too since things don't work the same way in c/c++ . thank you again .
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Any time that you have an expression like this

(b=false)

yes, the value false does get assigned to b. But the other effect is that the expression evaluates to the same value.

In other words

int x;
int y = (x = 3);

then both x and y take the value 3 because, for the y = statement (x = 3) evaluated to the value 3.

so (b = false) both set b to false AND evaluated to false.


------------------------
Bob
SCJP - 86% - June 11, 2009
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Originally posted by vaibhav panghal:
thank you krishnamoorthy i had this doubt too since things don't work the same way in c/c++ . thank you again .


Vaibhav, You are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean Que