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.
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.
Joined: Oct 04, 2004
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.
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.
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...
Joined: Oct 04, 2004
A HUGE THANK YOU! To all the responses I received above.
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.