wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Return Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return Question" Watch "Return Question" New topic
Author

Return Question

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Ok, I'm around 1/2 of the way through chapter 4 on Head First Java 2nd Edition. I really am having a hard time understanding returning values and what it's used for. I got the book around a week and a half ago, I've been studying Java in general for about a month. I never understand what returning values is for.
I understand what it means, for example, if you put "void" in the method name you can't return a value. Otherwise you can return values that have been declared.
Can someone explain to me, what this does exactly, why do you use it, when do you know to use it?

I appreciate it, I'm new to these forums as well.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch!

You use a "return" whenever... you need to return something. If you're performing a calculation or transformation, creating an object, anything. Sometimes you'll return an object you pass in. Sometimes you'll return the instance the method belongs to.

It's arguable that essentially all functions should return something (functional programming) rather than depending on side effects to do useful work.
subhash kumar
Ranch Hand

Joined: Jul 14, 2010
Posts: 63
your method is the behavior of class means it will perform something, Now how to see this behavior
1. By doing the calculation/operations/processing and returning the values.
2. By doing the calculation/operations/processing and returning nothing as void return type.
3. By passing the reference types as the parameter and processing it in the method and see the changed object where you called
this method.
it all depends the situations and what thins are available with you.
for example if you are using/making any utility method like "valueOf" then you need to return values from method.
if you are writing method for your own class needed in you module then you can go for 2nd or 3rd option.


Subhash Kumar
Attitude is everything
eric totte
Greenhorn

Joined: Aug 13, 2010
Posts: 10
----------------------------------------------------------------------------------------------
Case 1
----------------------------------------------------------------------------------------------

If you created the following method

public void add(int x, int y){
x+y;
}

using the code:

add(3,4);

would add the two values, but the sum would not be accessible for further use.

----------------------------------------------------------------------------------------------
Case 2
----------------------------------------------------------------------------------------------

public int add(int x, int y){
return x+y;
}

using the code;

int sum = add(2,3);

would add the two values, and store the calculation in the sum variable. Much more useful

Al Fraelich
Greenhorn

Joined: May 25, 2009
Posts: 20
Ben, the "return" is used to get the result of a method.
In it's simpilist form, you could have a method that let's say adds 2 to a number entered by the user. Of course something this simple could be done in the "main" method of the code, but as your programs get larger, you will want the "main" to be as concise and small as possible. So you will keep the methods seperate, and call them from the "main". Because methods are kept seperate from the main, you may need to "return" the result.

Al
 
jQuery in Action, 2nd edition
 
subject: Return Question