Java Reference
In-Depth Information
Java syntax: Typical procedure and function declarations
/** Comment describing what the procedure does */
public void method-name ( parameter-declarations ) { … }
Each parameter-declaration has the form type identifier (just like all vari-
able declarations). Adjacent parameter-declaration s are separated by com-
mas. There may be 0 parameter declarations. In a function declaration, key-
word void is replaced by the type of value that the function calculates.
Above, we made a point of including the specification of a method as part
of its definition. The specification is not needed for the program to compile and
run; it is just a comment. But clients of your code need the specification to under-
stand how to use the method. Get in the habit of always writing the specification
of a method before you write the method body.
Here is some motivation: during your programming career you will come to
rely heavily on the online Java API specifications. The online specifications were
automatically extracted from the comments in the code and turned into HTML
web pages using a tool called Javadoc ; those comments were written by pro-
grammers. Without those comments the API classes would be useless. When you
get a programming job you will have to write documentation: you will have no
choice! Also, if you learn to comment now, it will serve you well: programmers
who work with you will sing your praises.
Plus, you will get better grades.
In this chapter, we discuss mainly procedures and functions. A procedure
call is a statement: it is executed . A function call is an expression, with a type, so
it is evaluated . Throughout this chapter, we use the word method when dis-
cussing methods in general, and when we need to distinguish the two, we use
procedure or function .
Here is an example of a specification and header of a function.
Style Note
13.3.1:
method specs
Ad hoc polymorphism. Polymorphism , from a Greek word meaning multiform , means “capable
of having or occurring in several distinct forms”. When we write the calls
println(5) , println(b || c) , and println("xyz") , it looks like one proce-
dure println is able to handle arguments of many types, and if that were the
case, this would be an instance of parametric polymorphism . Instead, in Java,
one writes several different procedures with the same name but with different
parameter types —the procedure name is overloaded. Java is able to distinguish
which procedure to call based on the types of the arguments of the call. This is
known as ad hoc polymorphism . We will see other types of polymorphism later
on, for polymorphism is an important feature of OO languages.
Polymorphism in programming languages was first discussed by Christo-
pher Strachey in 1967, but the first major language that included polymor-
phism in a big way was Robin Milner's language ML, in 1976.
Search WWH ::




Custom Search