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 return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does return type "boolean" affect print statement in a method ?" Watch "How does return type "boolean" affect print statement in a method ?" New topic
Author

How does return type "boolean" affect print statement in a method ?

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371


In classes Monster, Vampire and Dragon return type of frighten() method is boolean and all frighten() methods have print statement. So how will frighten() method behave in different classes ?
In Monster class, frighten() method prints "arrrgh" and has return type true. What does this mean ? Will it print "arrrgh" and return true , does it mean that "arrrgh" will be printed on screen ?
In Vampire class, frighten() method prints "a bite" and has return type false. What does this mean ? Will it print "a bite" and return false , does it mean that "a bite" will not be printed on screen ?
In Dragon class, frighten() method prints "breath fire" and has return type true. What does this mean ? Will it print "breath fire" and return true , does it mean that "breath fire" will be printed on screen ?

If everytime it print satement is printed, irrespective of whether return is true or false, then whats the significance of return statement ? What is the effect of return "true" or "false" in frighten() method ?
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
In Monster class, frighten() method prints "arrrgh" and has return type true. What does this mean ? Will it print "arrrgh" and return true , does it mean that "arrrgh" will be printed on screen ?
In Vampire class, frighten() method prints "a bite" and has return type false. What does this mean ? Will it print "a bite" and return false , does it mean that "a bite" will not be inted on screen ?
In Dragon class, frighten() method prints "breath fire" and has return type true. What does this mean ? Will it print "breath fire" and return true , does it mean that "breath fire" will be printed on screen ?


Did you try running it?
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 58
For the first part as Sridhar said it is better you try executing your code and check the output

Return is to return a value from a method. It does not have any impact on the frighten method. You can as well make the method void, since you have declared that the method will return boolean, you need to return true/false.
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
Yes, I did and it produces output

a bite
breath fire
arrrgh.

My confusion is regarding return statement. What is the effect of "true" or "false" in the method(). I changed all of them as "true" and all of them as "false" and I am still getting same output. So what is the significance of return statement ? When shall I use "true" and when shall I use "false" in return statement ?


Secondly my confusion is regarding print statement. Usually, print statement is used with return type as "void". When its used with specific return type, does print statement get executed first and then the value is returned from method ? In this case, when "true" or "false" is returned, how and where is this return value used ?

Thanks
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Your frighten() methods all return a boolean value, you just don't save it into a variable or print it out. You could print it out (altho I don't see much sense in doing it)



The print statement prints out whatever you pass in as an argument. First it "converts" it into a String, by invoking .toString() method if you pass in an object reference. You can, and should, override the toString() method to get an output that makes more sense.



The output is:


First output is generic toString() from Object class, it's just the name of the class and it's position in memory. The second one is our own.

As for what executes first, the print statement or the method in the parenthesis ... in Java things get executed from most inward parenthesis out:

((1+2)/3 + (Math.abs(-2) - 2)) =
(3/3 + (2 - 2)) =
(1 + 0)=
1

Hope this clarifies things


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Hello here is an example of using the return statements of a method to have a different affect on the outcome. I've added an if statement in the main method. See if you can work out what is going on. Just let me know if you are not sure.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Here's the code with a few stylistic changes (originally I was just going to point out that writing "if (foo)" is preferred over "if (foo == true)"):
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does return type "boolean" affect print statement in a method ?