The Return StatementWhat is the return statement?Types of return statementCode ExamplesVoid type return statementCode ExampleBibliographySlide 8The Return StatementFrank GattiWhat 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 Examplespublic 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 Examplepublic 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/nutsandbolts/branch.html>•Fact Guru, Fact Guru, April 26, 2006 http://www.site.uottawa.ca:4321/java/index.html#returnstatementBibliographyCS124:Java, Section 3.4, David Eck, Hobart & William Smith’s Colleges, April 26, 2006
View Full Document