Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean Que

 
Hemant Gupt
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys I also had a similar doubt but now it is cleared because of your explanation
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic