Java Reference
In-Depth Information
is a method
invoked by that object. It may seem strange to spell an object name with a dot in it,
but that need not concern us for now.
When you use
is an object that is part of the Java language, and
System.out
println
for output, the data to be output is given as an
argument in parentheses, and the statement ends with a semicolon. The things you
can output are strings of text in double quotes, such as
System.out.println
;
"The changed string is:"
variables such as
; variables of other types such as variables of type
String
sentence
; and almost any other object or value. If you want to
output more than one thing, simply place an addition sign between the things you
want to output. For example:
; numbers such as
or
int
5
7.3
System.out.println("Answer is = " + 42
+ " Accuracy is = " + precision);
If the value of
is
, the output will be
precision
0.01
Answer is = 42 Accuracy is = 0.01
Notice the space at the start of
. No space is added automatically.
" Accuracy is = "
operator used here is the concatenation operator that we discussed earlier. So
the above output statement converts the number
The
+
to the string
and then forms
42
"42"
the following string using concatenation:
"Answer is = 42 Accuracy is = 0.01"
then outputs this longer string.
Every invocation of
System.out.println
ends a line of output. For example, consider the fol-
println
lowing statements:
System.out.println("A wet bird");
System.out.println("never flies at night.");
These two statements cause the following output to appear on the screen:
A wet bird
never flies at night.
If you want the output from two or more output statements to place all their out-
put on a single line, then use
print
versus
println
instead of
. For example, the statements
print
println
System.out.print("A ");
System.out.print("wet ");
System.out.println("bird");
System.out.println("never flies at night.");
Search WWH ::




Custom Search