Java Reference
In-Depth Information
Java syntax: print procedure call
System.out.print(
expression
);
Example
:
System.out.print(5);
Execution
: Place the value of the
expression
in the
Java console (which is a window or pane that con-
tains error messages and output from such
print
and
println
calls).
Java syntax: println procedure call
System.out.println(
expression
);
Example
:
System.out.println(5);
Execution
: Place the value of the
expression
in the Java console and then
start a new line (in the Java console).
2.3
Method bodies
You know about the caller's view of a method (as opposed to the writer's view),
and you know how to understand a method call. In this section, we investigate
the third part of a method definition, the method body, and discuss its execution.
2.3.1
The procedure body
A procedure body is a sequence of statements enclosed in braces
{}
. Here is an
example:
Style Note
13.2, 13.2.4:
indentation
conventions
/**
Print
b
,
c
, and
b+c
on separate lines.
*/
public static void
print3
(
int
b,
int
c) {
System.out.println(b);
System.out.println(c);
System.out.println(b+c);
}
This procedure body contains three statements. Notice the indentation:
• The opening brace
{
appears on the same line as the header;
• The sequence of statements is indented; and
• The closing brace
}
appears indented exactly under the header.
This convention is used by many Java programmers. We use it throughout the
text.
Note:
Class
java.lang.System
has in it a
static
variable
out
, which refers to
a
PrintStream
object.
PrintStream
objects deal with output and have a method
println
, which prints its argument, followed by a new-line character.
Execute this statement in your IDE:
System.out.println("Howdy");
Compiling and calling static methods
Every method needs to be inside a class, so in order to test
print3
we must
write a class in which to place it. Over the next few pages, we will write several
related methods, including
print3
. We will create a single class,
PrintExample
,
Search WWH ::
Custom Search