• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 51
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 67706
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
asserts have nothing at all to so with return values. Why are you mashing them together?
 
Jordan D. Williams
Ranch Hand
Posts: 51
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 27675
89
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 27675
89
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Sheriff
Posts: 17539
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jordan D. Williams
Ranch Hand
Posts: 51
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
reply
    Bookmark Topic Watch Topic
  • New Topic