Java Reference
In-Depth Information
Data Output
Data output in Java is easier to implement than data input. The out object,
which is part of the System class, can be used to display program data di-
rectly.TheSystemclassispartofthejava.langlibrary.Theoutobjectofthe
System class allows you to use the print() and println() methods of the
PrintStream class, located in java.io. In the HelloJava program, developed
in Chapter 2, we used the println() method to display a message on the
screen. The following expression is part of the HelloJava.java program:
System.out.println("Hello World, this is Java");
The println() method automatically terminates the displayed line. The
result is that the current text output marker, sometimes called the cursor ,
is moved to the next screen line automatically. The print() method, on the
other hand, sends data to the video display at the current position of the
text output marker, but does not index to the next screen line. For exam-
ple, the statements:
System.out.print("value");
System.out.print("
number");
System.out.print("
code");
System.out.flush();
Produce the following output:
value
number
code
In this case, the flush() method, also of the PrintStream class, is used
to terminate the line.
Escape characters
The Java display methods, print() and println(), recognize special charac-
tersthatservetodelimitandformatthestringorcharactertobedisplayed;
also to display symbols that are used in the statement grammar. For exam-
ple, the “ symbol is used in print() and println() to markthe beginning and
the end of the string to be displayed. If you were to include this symbol as a
character, then the processing logic would be unable to correctly format
the output. Suppose you wanted to display on the screen a message that
contained a word inside quotation marks, for example:
She said her name was "Mary"
Since quotation marks are used to end the string to be displayed, the
following statement would not execute correctly
System.out.println("She said her name was "Ellen"");
Search WWH ::




Custom Search