| 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
|
|
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
|
|
|
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
|
|
. . . 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
|
|
|
That's a pleasure
|
 |
 |
|
|
subject: if - boolean
|
|
|