• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning and using booleans

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant believe how much time ive spent trying to use Java's Booleans. In my opinion, C++ was much simpler here. First, what is the difference, if any, between a Boolean and a boolean? Can i even declare (or construct) or A boolean?
What i would like to do is use a test function that test a condition or two and then returns a Boolean(or is it boolean?) indicating whether the test condition was true or false. Here's what I've tried so far.
private Boolean test(some arguments)
{ if(condition)
return new Boolean("True");
//Ive also tried return TRUE and return FALSE
else return new Boolean("False");
//Ive tried return new Boolean(1) and return new Boolean(0)
}
When this functions returns its value I would like to use it in an IF statement. It seems that Java only accepts Booleans (or booleans?) expressions in an IF test conditon.
Do I really need booleans? If i do, whats the correct way to use them here?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, what is the difference, if any, between a Boolean and a boolean? Can i even declare (or construct) or A boolean?

A boolean is a primitive that can be initialized to "true" or "false" (without the quote marks). A Boolean is a wrapper object used when you have a boolean and need an object. The two are not the same.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here's what I've tried so far.
private Boolean test(some arguments)
{ if(condition)
return new Boolean("True");
//Ive also tried return TRUE and return FALSE
else return new Boolean("False");
//Ive tried return new Boolean(1) and return new Boolean(0)
}

When this functions returns its value I would like to use it in an IF statement. It seems that Java only accepts Booleans (or booleans?) expressions in an IF test conditon.

Do I really need booleans? If i do, whats the correct way to use them here?


Yes, you really need booleans. Try this.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or you could simply write this:

This returns whatever boolean value the condition evaluates to

 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely better, Junilu. I was trying to stick fairly close to the code he gave so he could see the changes, but I would definitely use yours in real life.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic