• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Boolean Que

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys I also had a similar doubt but now it is cleared because of your explanation
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic