• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about boolean and 'return' statement  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!