Getting Started With Java – String (text) Output in Java
Even with our simple program, we can introduce some variations on output that will prove useful with
most of our future Java programs.
1. To print a single blank line, use the line
System.out.println();
2. Some strings are quite long, and will not fit on one line of a program (or they will run off the
screen). To split a string across multiple lines of code, write something like the following:
System.out.println("This will print a verrrrrry long"
+ " string on one line by joining"
+ " these strings into one string");
3. Each println statement will advance the printing position to the next line. If we wish to stay on
the same line, at the end of the last printed character, use the print statement instead.
4. Each string is denoted using double quotes ("), which begs the question, “How do you print double
quotes?” You can force Java to interpret a double quote as a printable character, rather than a
string delimiter, by using the backslash (\) character.
System.out.println("He said, \"That's ridiculous.\"");
The following program illustrates some uses of print and println.
class PrintDemo
{
public static void main (String[] args)
{
System.out.println("Never put off till tomorrow");
System.out.println();
System.out.print("what you can do");
System.out.println(" the day");
System.out.println();
System.out.println("after" + " " + "tomorrow.");
}
}
This will produce the following output:
Never put off till tomorrow
what you can do the day
after tomorrow
Using a slash (\) to precede another character and produce a different result is known as an escape
sequence. Some examples of escape sequences are as follows:
Character (without slash) Escape Sequence (with slash)
" a String delimiter \" a printable quotation mark
\ starts an escape sequence \\ a printable slash
n the letter 'n', printable \n prints a new line
Getting Started With Java – String (text) Output in Java
Exercises
1. Write a Java program to print your name and address as they would appear on the envelope of a
letter addressed to you at your home. For example,
Your Name
123 Address Ave.
Ottawa, ON K2L 4G7
2. What does this program print? (Determine the output without programming it first!)
class Advice
{
public static void main (String[] args)
{
System.out.print("If at first ");
System.out.println("you don't succeed" + ",");
System.out.print("failure may be ");
System.out.println("your style");
}
}
3. Rewrite the following program using the indentation style (formatting) shown in our previous
examples.
class Quote{public static void main (String[] args) {
System.out.print("The unexamined life");
System.out.println(" is not worth living.");}}
4. Write the Java statements that would print the following exactly as shown.
(Hint: The backslash character, \, needs special treatment just like the quotation character, ")
A (forward) slash is "/"
while
a backslash is "\"
5. The following program contains a number of errors. Rewrite the program with the errors corrected.
class BadForm
public void main (string[] args);
{
System.Out.Println('What's wrong with this?')
}