Java Reference
In-Depth Information
println
Output
You can output one line to the screen using
System.out.println
. The items that are
output can be quoted strings, variables, numbers, or almost any object you can define in
Java. To output more than one item, place a plus sign between the items.
SYNTAX
System.out.println(
Item_1
+
Item_2
+ ... +
Last_Item
);
EXAMPLE
System.out.println("Welcome to Java.");
System.out.println("Elapsed time = " + time + " seconds");
They produce the same output as our previous example:
A wet bird
never flies at night.
Notice that a new line is not started until you use
println
, rather than
print
. Also
notice that the new line starts
after
outputting the items specified in the
println
. This
is the only difference between
print
and
println
.
println
versus
print
The only difference between
System.out.println
and
System.out.print
is that
with
println
, the
next
output goes on a
new line
, whereas with
print
, the next output is
placed on the
same line
.
EXAMPLE
System.out.print("Tom ");
System.out.print("Dick ");
System.out.println("and ");
System.out.print("Harry ");
This produces the following output:
Tom Dick and
Harry
(The output would look the same whether the last line reads
print
or
println
.)
Another way to describe the difference between
print
and
println
is to note that
System.out.println(
SomeThing
);
is equivalent to
System.out.print(
SomeThing
+ "\n");