Returning values from
methods in Java
Getting a return value from a
method
• Methods that give information back to their callers are
said to return a value
• Must have a return type (not void) to do so.
• Once you know what value should be given back, use a
return statement
– The word return, followed by the value to return, followed by
a semicolon.
– The type of the value returned must match the return type.
– When a return statement is executed, the method ends
immediately, even if there is more to execute.
Example
• Our method to average two numbers looks like this
public static double Average(double
num1, double num2) {
double avg = (num1+num2) / 2;
return avg;
}
• The type of the value returned (the double local variable
called avg) matches the method’s return type.
• When the return statement is encountered, the method
ends.
Example cont’d
• If we were to add a line after the return:
public static double Average(double
num1, double num2) {
double avg = (num1+num2) / 2;
return avg;
System.out.print(avg);
}
• The above function will not print anything
– The System.out.print statement will never execute because the
method exited on the previous line.
Using a method
• To use a static method, class name, followed by a
period, the method name, and the parameters
– Methods with a return value expect the value to be used in
some way:
double x=Math.sqrt(2);
– Separate multiple parameters by commas:
double y=myClass.Average(num1,num2);
– Methods declared void expect no return value to be used:
System.out.print(“No return value”);
Using a Method Cont’d
• Types of parameter must match types of
parameters in the method declaration
– For example can’t send strings to our Average method:
myClass.Average(“A”,”B”);//Error!
• Can declare a method with no parameters
– Use open and close parentheses as a parameter list
– double myDouble = Math.random();
Review Question 1
• Give a Java statement that passes 6 as a
parameter to the xxx method in the YYY
class.
YYY.xxx(6);
Review Question 2
• Give a Java statement that passes 9 and 14
as parameters to the aaa method in the BBB
class and sets the variable z to the result.
z=BBB.aaa(9,14);
Review Question 3
• Give a Java statement that passes the string “PQ”
as parameters to the sss method in the TTT class
and passes the result as a parameter to the rrr
method in the TTT class. No result is returned
from rrr.
TTT.rrr(TTT.sss(“PQ”));
• How can you tell that rrr returns no result?
No variable is set to it, it is not passed to another
method, used in an expression, etc.
Review Question 4
• Give a Java statement that calls the nnn method
in the MMM class passing it no parameters, and
prints the result.
System.out.print(MMM.nnn());
Review Question 5
• Call the hhh method in the GGG class, passing it
4.7 as a parameter. Concatenate the string “XYZ”
to the result and assign that value to variable s.
s=GGG.hhh(4.7)+”XYZ”;
Method Example: What does this do?
Class X { p:
public static void a(String p){
q:
System.out.println(“=“+p);}
public static String b(String q){
r:
return q+q;} s:
public static void main(String[] t:
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
r set to xy, t set to w
Class X { p:
public static void a(String p){
q:
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s:
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Passes “xy” to method b
Class X { p:
public static void a(String p){
q:
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s:
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
q becomes “xy”
Class X { p:
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s:
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method b returns xyxy
Class X { p:
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s:
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
s set to value returned by b
Class X { p:
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
“xyxy” passed to method a
Class X { p:
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
p becomes “xyxy”
Class X { p: xyxy
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”;
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a outputs =xyxy
Class X { p: xyxy
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a returns control after call
Class X { p: xyxy
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method b is passed “w”
Class X { p: xyxy
public static void a(String p){
q: xy
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
q becomes “w”
Class X { p: xyxy
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method b returns “ww”
Class X { p: xyxy
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
“ww” passed to method a
Class X { p: xyxy
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
p becomes “ww”
Class X { p: ww
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a outputs =ww
Class X { p: ww
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a returns control after call
Class X { p: ww
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method b is passed “z”
Class X { p: ww
public static void a(String p){
q: w
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
q becomes “z”
Class X { p: ww
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method b returns “zz”
Class X { p: ww
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
“x” is added to “zz” and passed to a
Class X { p: ww
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
p becomes “xzz”
Class X { p: xzz
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a outputs =xzz
Class X { p: xzz
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
=xzz
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method a returns control after call
Class X { p: xzz
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
=xzz
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Method main completes
Class X { p: xzz
public static void a(String p){
q: z
System.out.println(“=“+p);}
public static String b(String q){
r: xy
return q+q;} s: xyxy
public static void main(String[] t: w
args){ Output:
String r="xy", t=“w”; =xyxy
String s=X.b(r);
=ww
X.a(s);
=xzz
X.a(X.b(t));
X.a(“x”+X.b("z"));}}
Summary
• To pass information to a method
– Declare parameters with types and names
– Separate by commas
• To pass information back from a method
– Declare the method with a return type
– Use a return statement
– Method ends when return statement executes
• To call a static method, class name, period, method
name, parameters inside parentheses
– Parameters much match the types in the method definition
– Can have an empty parameter list ()