Java Reference
In-Depth Information
EXAMPLE 2-21
Consider the following Java statements:
2
System.out.print("Hello there.\nMy name is James.");
or:
System.out.print("Hello there.");
System.out.print("\nMy name is James.");
or:
System.out.println("Hello there.");
System.out.print("My name is James.");
In each case, the output of the statements is:
Hello there.
My name is James.
EXAMPLE 2-22
Suppose you want to output the following sentence in one line as part of a message:
It is sunny, warm, and not a windy day. We can go golfing.
Obviously, you will use the methods print and/or println to produce this
output. However, in the programming code, this statement may not fit in one line as part
of the output statement. Of course, you can use more than one output statement, as follows:
System.out.print("It is sunny, warm, and not a windy day. ");
System.out.println("We can go golfing.");
Two output statements are used to output the sentence in one line. You can also use the
following statement to output this sentence:
System.out.println("It is sunny, warm, and not a windy day. " +
"We can go golfing.");
In this statement, note that because there is no semicolon at the end of the first line, this
output statement continues at the second line. Also, note that the first line is followed by
the operator + , and there is a double quotation mark at the beginning of the second line.
The string is broken into two strings, but both strings are part of the same output statement.
If a string appearing in an output statement is long and you want to output the string in
one line, you can break the string by using either of these two approaches. However, the
following statement using the Enter (or return) key would be incorrect:
System.out.println("It is sunny, warm, and not a windy day.
We can go golfing.")
 
Search WWH ::




Custom Search