• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

if(boolean)

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, It is typographical error. There is no semicolon at the end of the if statement.

Correct one:
if((int) (Math.random() * 2) ==0)
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an aside,

if (x) return true; else return false;

can be nicely shortened to

return x;
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic