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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if(boolean)" Watch "if(boolean)" New topic
Author

if(boolean)

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129


What is program logic behind the above pgm.In the getBoolean(), we are checking whether the number is generated randomly is zero. If so, we return false elase we return true. Correct me, if i am wrong.

Whether the if statment returns the boolean expr. that is if(boolean) is possible in java. I think I am messing up something?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


if((int) (Math.random() * 2) ==0);

This particular conditional statement does nothing. The clue as to why is in bold. (It shouldn't even compile).
[ August 11, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Sorry, It is typographical error. There is no semicolon at the end of the if statement.

Correct one:
if((int) (Math.random() * 2) ==0)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Originally posted by Niyas Ahmed Sheikh:


What is program logic behind the above pgm.In the getBoolean(), we are checking whether the number is generated randomly is zero. If so, we return false elase we return true. Correct me, if i am wrong.

Whether the if statment returns the boolean expr. that is if(boolean) is possible in java. I think I am messing up something?


(int) (Math.random() * 2) will return either 0 or 1, therefore having getBoolean() return true or false depending on the returned value is simple a way of generating a random boolean value.


Joanne
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
As an aside,

if (x) return true; else return false;

can be nicely shortened to

return x;


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
In the getBoolean(), we are checking whether the number is generated randomly is zero. If so, we return false; else we return true. Correct me, if i am wrong.

true.

Whether the if statment returns the boolean expr. that is if(boolean) is possible in java.

"if (boolean)" is not only possible in Java, it is the only way to write an if statement. "if (0)" (or any other int) will not compile.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if(boolean)