23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Abstract methods may only appear with<strong>in</strong> an abstract class. We discuss the<br />

usefulness of this construct <strong>in</strong> Section 2.4.<br />

• f<strong>in</strong>al: This is a method that cannot be overridden by a subclass.<br />

• static: This is a method that is associated with the class itself, <strong>and</strong> not with<br />

a particular <strong>in</strong>stance of the class. Static methods can also be used to change the<br />

state of static variables associated with a class (provided these variables are not<br />

declared to be f<strong>in</strong>al).<br />

Return Types<br />

A method def<strong>in</strong>ition must specify the type of value the method will return. If the<br />

method does not return a value, then the keyword void must be used. If the return<br />

type is void, the method is called a procedure; otherwise, it is called a function. To<br />

return a value <strong>in</strong> <strong>Java</strong>, a method must use the return keyword (<strong>and</strong> the type<br />

returned must match the return type of the method). Here is an example of a method<br />

(from <strong>in</strong>side the Gnome class) that is a function:<br />

public booleanisMagical () {<br />

}<br />

returnmagical;<br />

As soon as a return is performed <strong>in</strong> a <strong>Java</strong> function, the method ends.<br />

<strong>Java</strong> functions can return only one value. To return multiple values <strong>in</strong> <strong>Java</strong>, we<br />

should <strong>in</strong>stead comb<strong>in</strong>e all the values we wish to return <strong>in</strong> a compound object,<br />

whose <strong>in</strong>stance variables <strong>in</strong>clude all the values we want to return, <strong>and</strong> then return a<br />

reference to that compound object. In addition, we can change the <strong>in</strong>ternal state of<br />

an object that is passed to a method as another way of "return<strong>in</strong>g" multiple results.<br />

Parameters<br />

A method's parameters are def<strong>in</strong>ed <strong>in</strong> a comma-separated list enclosed <strong>in</strong><br />

parentheses after the name of the method. A parameter consists of two parts, the<br />

parameter type <strong>and</strong> the parameter name. If a method has no parameters, then only<br />

an empty pair of parentheses is used.<br />

All parameters <strong>in</strong> <strong>Java</strong> are passed by value, that is, any time we pass a parameter to<br />

a method, a copy of that parameter is made for use with<strong>in</strong> the method body. So if<br />

we pass an <strong>in</strong>t variable to a method, then that variable's <strong>in</strong>teger value is copied.<br />

The method can change the copy but not the orig<strong>in</strong>al. If we pass an object reference<br />

as a parameter to a method, then the reference is copied as well. Remember that we<br />

can have many different variables that all refer to the same object. Chang<strong>in</strong>g the<br />

37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!