wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Returning and using booleans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning and using booleans" Watch "Returning and using booleans" New topic
Author

Returning and using booleans

Reid Hattaway
Greenhorn

Joined: Jul 15, 2001
Posts: 7
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Feb 26, 2001
Posts: 4447
    
    5

or you could simply write this:

This returns whatever boolean value the condition evaluates to


Junilu - [How to Ask Questions] [How to Answer Questions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.
 
 
subject: Returning and using booleans
 
Similar Threads
help moderator or site admin
Boolean unboxing.
How to read a getter from javascript
Using Objects as Parameters
while( )