aspose file tools
The moose likes Beginning Java and the fly likes if - boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "if - boolean" Watch "if - boolean" New topic
Author

if - boolean

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
This code compiles and runs with NO output. Cannot understand why ?



Regards,<br />Arnab
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

What were you expecting? The print statement is in a condition that is set to false.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670

Normally an if statement would not compile if you replace == with =. However in this case it compiles, because b is boolean. An assignment (=) has the side effect of returning the value assigned, which means for example

Here the last statement assign y = 1, and then assigns x = 1. Because (y = 1) not only assigns y = 1; it also evaluates to 1. So

is the same as

Similarly,

is the same as

[ November 27, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
You don't need to use = or == in the if () statement when dealing with a boolean primitive variable. You just write
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
. . . and I forgot you want it to work if myBoolean is false. Then all you have to write is,

CR
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Thank you everyone.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
That's a pleasure
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: if - boolean
 
Similar Threads
Refering to another class's instance of a different class, inside a class
please ans. this question regarding interface
return in catch block?
boolean return question
please help answer the mcq