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.

1.2 Methods<br />

Methods <strong>in</strong> <strong>Java</strong> are conceptually similar to functions <strong>and</strong> procedures <strong>in</strong> other<br />

highlevel languages. In general, they are "chunks" of code that can be called on a<br />

particular object (from some class). Methods can accept parameters as arguments, <strong>and</strong><br />

their behavior depends on the object they belong to <strong>and</strong> the values of any parameters<br />

that are passed. Every method <strong>in</strong> <strong>Java</strong> is specified <strong>in</strong> the body of some class. A<br />

method def<strong>in</strong>ition has two parts: the signature, which def<strong>in</strong>es the <strong>and</strong> parameters for<br />

a method, <strong>and</strong> the body, which def<strong>in</strong>es what the method does.<br />

A method allows a programmer to send a message to an object. The method signature<br />

specifies how such a message should look <strong>and</strong> the method body specifies what the<br />

object will do when it receives such a message.<br />

Declar<strong>in</strong>g Methods<br />

The syntax for def<strong>in</strong><strong>in</strong>g a method is as follows:<br />

modifiers type name(type 0 parameter 0 , …, type n−1 parameter n−1 ) {<br />

}<br />

// method body …<br />

35

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

Saved successfully!

Ooh no, something went wrong!