File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How does 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 "How does "return" work for a method with a "boolean" return type?" Watch "How does "return" work for a method with a "boolean" return type?" New topic
Author

How does "return" work for a method with a "boolean" return type?

Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Hello there!

I am systematically going through the Java Tutorials and I came up to something I don't quote understand. I am looking at the answers for question #2. Here is a direct link to the question. It's a little frustrating that they have not even mentioned assertion when they ask us readers to do this, but that's OK.

Basically I am looking at the Card.java class and I see this:



ACE and KING are of type int.

This is what I *think* is happening here. Please correct me if I am wrong. Basically the code that follows the return statement is like an if expression to be evaluated - except it does not have parentheses around it. If the conditions are true (ACE is less than or equal to rank AND rank is less than or equal to KING) then the method will return true. If the one of the conditions is NOT true, then the method will return false. Is that correct so far?

If someone could shed some might for me and others who might be confused about this, it would be greatly appreciated!

Thanks so much for all your time and effort!


John 3:16
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

asserts have nothing at all to so with return values. Why are you mashing them together?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Bear Bibeault wrote:asserts have nothing at all to so with return values. Why are you mashing them together?


Well... the Java Tutorials example used boolean return type fuctions whose return statement I did not understand and those same methods were used with assert. I thought that putting them together would give people a better handle of the context in which the question occurred. I can separate them if you think it would be better.

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Unfortunately the kind of question which looks like "How does feature A work when you use it with feature B" is almost always answered by "The same way it works when you use it in any other context". And so it is here. Methods which return boolean data as their value do so whether or not that value is going to be inspected by an assert statement.

So I don't think that kind of question is really useful. It just leads people to jumble up different concepts into a big heap, whereas what should happen is that beginners learn early on that you don't need to look at everything all at once. Looking at small pieces (e.g. what is a boolean value, which is one of your questions, and a perfectly good question at that) is much easier when you don't have to worry about how those small pieces are going to be put together into a larger structure.
Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Paul Clapham wrote:... Looking at small pieces (e.g. what is a boolean value, which is one of your questions, and a perfectly good question at that) is much easier when you don't have to worry about how those small pieces are going to be put together into a larger structure.


What I asked previously and the way I formulated it was really not a very well thought out approach. I edited the question and the topic title. Is that better?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jordan D. Williams wrote:Basically the code that follows the return statement is like an if expression to be evaluated - except it does not have parentheses around it. If the conditions are true (ACE is less than or equal to rank AND rank is less than or equal to KING) then the method will return true. If the one of the conditions is NOT true, then the method will return false. Is that correct so far?


Yes, that's right. It's called a "boolean value", and its value can only be true or false. The if-statement also requires a boolean value in the parentheses after the "if" keyword, so you've made a valid connection there.

Here's a link to the tutorial about primitive data types. It mentions the boolean data type, but that's about all it does. It kind of assumes you already know what it means and why you would need a boolean value, but from what I've seen that isn't necessarily a safe assumption with regard to beginning programmers. Basically you need a boolean value if you want to keep track of whether some condition is (or was) true or not.

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Jordan D. Williams wrote:This is what I *think* is happening here. Please correct me if I am wrong. Basically the code that follows the return statement is like an if expression to be evaluated - except it does not have parentheses around it. If the conditions are true (ACE is less than or equal to rank AND rank is less than or equal to KING) then the method will return true. If the one of the conditions is NOT true, then the method will return false. Is that correct so far?

Your understanding is basically correct. This kind of expression is called a boolean expression: some logic is performed, and the expression is evaluated to a boolean. We could write a method that looks, one by one, at each condition we are interested in:


Instead, this method compacts all that into a single statement.

Something else to point out is the use of the short-circuiting AND operator, '&&'. In order for an AND expression to return true, the expressions on both sides of the operator must themselves evaluate to true. If the value to the left of the operator is false, then there is no need to evaluate the rest of the statement. So if we see that rank is less than ACE, we can return false immediately. That's exactly what the short-circuiting operators do.

Edit: too, too slow....
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Dennis Deems wrote:Edit: too, too slow....
It happens...

As a consolation, you correctly pointed out that it's a boolean expression that evaluates to a boolean value of either true or false.

One of my pet peeves when doing code reviews is the kind of code in your "long" example.


In the short form given above, the parentheses are optional but I add them anyway because I feel it makes it clearer that it's the result of a boolean expression that's returned; at least that's how it feels in my head.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Wow! Every day I post/read on these forums someone blows my mind!

Junilu Lacar wrote:


That is just awesome! I really have no other comments about that!

Thanks so much for the help all!
 
wood burning stoves
 
subject: How does "return" work for a method with a "boolean" return type?
 
Similar Threads
Enumerated type
Accessor methods on enum
Compare an Object to an Enum list
doubts in enum and generalized code
class question