aspose file tools*
The moose likes Beginning Java and the fly likes if - boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 18101
    
  39

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: 18671

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: 36453
    
  15
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: 36453
    
  15
. . . 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: 36453
    
  15
That's a pleasure
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if - boolean
 
Similar Threads
boolean return question
please ans. this question regarding interface
Refering to another class's instance of a different class, inside a class
please help answer the mcq
return in catch block?