This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Boolean and boolean 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 "Boolean and boolean" Watch "Boolean and boolean" New topic

Boolean and boolean

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I understand that Boolean is a class and boolean is of type primative?

Looking at the following method declaration:

public Boolean withdraw (amount double)

return x;

should it therefore not be declared as:

public boolean withdraw (amount double)
return x;

Which means the first method declaration was a typing error i.e. using a capital B in stead of a lower case B, for boolean?
Russ Ray
Ranch Hand

Joined: Feb 11, 2003
Posts: 52

I am not sure what you are asking........ There are reasons why one would select returning an Object over a primitive. Ask yourself, where is the results being returned? I think this will answer your question. I hope I understood correctly.

Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
It is not necessarily a typo. It depends on wether there is a need to wrap the boolean type into its class representation Boolean. If there isn't a good reason for doing this I would refactor the method to return a boolean.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your replies.

Mmmm. I have been told to use the method declaration:

public Boolean withdraw (double amount)

Until an earlier response in another thread I didn't realise there was a class Boolean. Up to this point I have only used boolean. I also haven't covered wrapper class? (Any pointers on when to use this would be great! I have looked in the API but it doesn't give examples of when to actually use this).

Up to this point in my studies I have declared a variable at the top of my class i.e. boolean permit = false; then used an if statement in a method to return true or false.

On this occasion I'm also using an if statement to return true of false BUT the method declaraction is to return a Boolean not boolean.

I'm I losing it here?
Henry Wong

Joined: Sep 28, 2004
Posts: 18550

You are returning the variable "x" in the method. Whether the method declaration is Boolean or boolean would depend on what type the "x" variable is.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Its easy to convert from Boolean to boolean:

Of course this leaves you open to NullPointerExceptions if blah happened to be null, so a better way is this:

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Russ Ray
Ranch Hand

Joined: Feb 11, 2003
Posts: 52

There are two examples where I know you would use the Boolean class instead of a primitive. The first is when you need a Serializable object, such as passing it over a network. Another time I can think of is when you are coding in a J2EE environment and using a database.

Let me explain. When we communicate over networks, we serialize and deserialize objects into byte streams so we can pass it around the wire. Once on the other end of the communication, the JVM rebuilds the object. If you were using a primitive, you would get a runtime exception where you tried to return it over the wire.

The other example is when using the J2EE and certain databases. Using this setup, primitives can not have a "null" value, yet databases can store a "null" value in a primitive field. When we fetch a value, which is "null" and a primitive, the database converts the value to the default value for the primitive. When we write to the database, J2EE determines if the data was changed (dirty) in any operation. If it has, it writes to the database. The problem is: when the database value is checked, the value is "null" and does not correspond to the value read into the system. The two values are not the same; therefore J2EE throws a runtime exception. Using a Boolean in the database and application limits this problem.

Hope this helps you.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

In addition to the reasons Russ indicates, here's a list on when to use type wrappers that I found on the web. It seems like a fairly accurate listing.

Wrappers are used to enclose a simple datatype or a more primitive object into an object. This is sometimes necessary because:
  • Simple datatypes are not part of the object hierarchy.
  • Simple datatypes are passed by value and not by reference.
  • Two methods can't refer to the same instance of a simple type.
  • Some classes can only use members of another class and not a simple type.
  • The wrapped object lacks advanced methods needed for object manipulation

  • In addition, there is a fairly good discussion/tutorial on Wrapper classes for primitive types available at

    As for whether you should use a Boolean object, or a boolean primitive for the return type, personally I would not assume that it is a typo. The best advice I can give would be to ask your instructor for clarification on the specification (i.e. the method declaration you have been told to use) for the assignment. Since you have not yet covered the primitive wrappers in your course yet, there is a chance that it is a typo and it should be boolean. On the other hand, since attention to detail is key to being a good developer, it could just as easily be a "test" to see how many students notice it. Or is could just be an oversight on your instructors part. So it is probably best to ask for clarification...
    Maureen Charlton
    Ranch Hand

    Joined: Oct 04, 2004
    Posts: 218
    A HUGE THANK YOU! To all the responses I received above.

    As suggested:
    ask your instructor for clarification

    This I did! The method declaration was suppose to be with a lower case 'b' Not a capital 'B' for the word boolean. (What a difference this makes!)

    public Boolean withdraw (double amount)
    This declaration refers to a Boolean class

    public boolean withdraw (double amount)
    This method declaration refers to a boolean primative

    It's been a worthwhile exercise, although not intended! Many thanks to you ALL.
    I agree. Here's the link:
    subject: Boolean and boolean
    Similar Threads
    Help on testprogram and subclass please
    return Boolean
    Need help with username and password validation
    Interfaces and Filter Assignment
    inheritance and variables