Java Reference
In-Depth Information
Section 6.4 Declaring Methods with Multiple Parameters
• When a method is called, the program makes a copy of the method's argument values and assigns
them to the method's corresponding parameters. When program control returns to the point in
the program where the method was called, the method's parameters are removed from memory.
• A method can return at most one value, but the returned value could be a reference to an object
that contains many values.
• Variables should be declared as fields of a class only if they're required for use in more than one
method of the class or if the program should save their values between calls to the class's methods.
• When a method has more than one parameter, the parameters are specified as a comma-separated
list. There must be one argument in the method call for each parameter in the method declara-
tion. Also, each argument must be consistent with the type of the corresponding parameter. If a
method does not accept arguments, the parameter list is empty.
String s can be concatenated (p. 208) using operator + , which places the characters of the right
operand at the end of those in the left operand.
• Every primitive value and object in Java can be represented as a String . When an object is concat-
enated with a String , the object is converted to a String , then the two String s are concatenated.
•If a boolean is concatenated with a String , the word "true" or the word "false" is used to rep-
resent the boolean value.
• All objects in Java have a special method named toString that returns a String representation
of the object's contents. When an object is concatenated with a String , the JVM implicitly calls
the object's toString method to obtain the string representation of the object.
• You can break large String literals into several smaller String s and place them on multiple lines
of code for readability, then reassemble the String s using concatenation.
Section 6.5 Notes on Declaring and Using Methods
• There are three ways to call a method—using a method name by itself to call another method of
the same class; using a variable that contains a reference to an object, followed by a dot ( . ) and
the method name to call a method of the referenced object; and using the class name and a dot
( . ) to call a static method of a class.
• There are three ways to return control to a statement that calls a method. If the method does not
return a result, control returns when the program flow reaches the method-ending right brace or
when the statement
return ;
is executed. If the method returns a result, the statement
return expression ;
evaluates the expression , then immediately returns the resulting value to the caller.
Section 6.6 Method-Call Stack and Stack Frames
• Stacks (p. 209) are known as last-in, first-out (LIFO) data structures—the last item pushed (in-
serted) onto the stack is the first item popped (removed) from the stack.
• A called method must know how to return to its caller, so the return address of the calling meth-
od is pushed onto the method-call stack when the method is called. If a series of method calls
occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that
the last method to execute will be the first to return to its caller.
• The method-call stack (p. 210) contains the memory for the local variables used in each invoca-
tion of a method during a program's execution. This data is known as the method call's stack
Search WWH ::




Custom Search