Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning and using booleans

 
Reid Hattaway
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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?
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • 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: 9059
12
  • Mark post as helpful
  • send pies
  • 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.

 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 9059
12
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic