• 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
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt about boolean and 'return' statement

 
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I've a doubt about this code:



My doubts are about the 'return' statement. What's the difference of false and true? The output is printed anyway.

Thanks for replies!
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try changing your for loop to something like:



Note: it's early here and i've not had coffee. The above may have typos or other errors.

The point is that the returned value is used OUTSIDE the method, after it has been called.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in case of Vampire class, the bool value is false so the exact string is "That's lame". Right?
 
fred rosenberger
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Addeo wrote:So in case of Vampire class, the bool value is false so the exact string is "That's lame". Right?

This question is not correct. And other mistakes in it.

Java has data type "boolean", not "bool" (the latter would cause you a compile error). To say "exact string is..." is not correct. There are two print statements which would be executed accordingly to the returned value of method "frighten".
Nevertheless, in all cases two print statements would be executed in fred's given example. One from the "frighten" method no matter which value is returned, and one from the code snippet within the loop itself.

As fred suggested, you should give a try to execute and see what happens. P.S. as fred already mentioned in his provided code snippet could be some errors, there is one missing semicolon, I shall leave that to find out for yourself
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're perfectly right, i know bool is not right, i was simply shortening :P

So i tryed the code and as you said, all strings popped out. Now my question is, what's the effective sense and utility of those 'return' statement in this case?
 
fred rosenberger
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it lets you do different things depending on what is returned. Your method might be better named as "isFrightening". You're asking a question, and getting an aswer.


 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but in the start code they 're useless. But without the 'return(s)', the boolean methods won't compile.

This have much more sense, but i know the sense of the start code was different :P

 
Sheriff
Posts: 67671
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
If the return values of the methods aren't used, why do they return a value at all?

And don't "shorten" things. Development is all about preciseness and clarity.
 
Lookout! Runaway whale! Hide behind this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic