Return Statement

Download Report

Transcript Return Statement

The Return Statement
Frank Gatti
What is the return statement?
• It is a type of statement.
• The return statement is a control flow
statement that terminates the the execution of a
method and returns control to it’s caller.
• The purpose of a control flow statement is to
control the flow of execution of a program.
• It generally should not be more than one line long.
Types of return statement
• Return statement that returns a value.
– If the return type of a method is not void, all the paths
of the method body must be terminated by a return
statement with an expression that matches the return
type.
Code Examples
public final int ordinal;
public final String name;
public String toString(){
return name;
}
Public int getOrdinal(){
return ordinal;
}
Void type return statement
• Return statements that do not return a value.
– If the return type of a method is void, no return
statements in the method body may return values.
Code Example
public void setDate(Date date){
this.date = date;
return;
}
• If a method type is void, the return is not required.
Bibliography
• Jia, Xiaoping, Object Oriented Software
Development Using Java. Addison Wesley, 2003
• The JavaTM Tutorial, Sun Microsoft Systems Inc,
April 25, 2006,
<http://java.sun.com/docs/books/tutorial/java/nutsand
bolts/branch.html>
• Fact Guru, Fact Guru, April 26, 2006
http://www.site.uottawa.ca:4321/java/index.html#retur
nstatement
Bibliography
CS124:Java, Section 3.4, David Eck, Hobart &
William Smith’s Colleges, April 26, 2006
<http://math.hws.edu/eck/cs124/javanotes2/c
3/s4.html>