Search 72,712 tutors
FIND TUTORS

Embedding line breaks in Java literal Strings

When creating output from a Java program, you can control where line breaks occur in the text that you output by using the println method, which puts a line break at the end of whatever text is being output by the call.
 
However, you may need to insert line breaks into strings that you are creating, such as in the toString method of a class. How do you do it?
 
First, note that when creating String literals, you can insert a variety of special characters using escape sequences.
 
An escape sequence is a sequence of characters, starting with a backslash ("\"). When the reader that is constructing your string encounters a backslash, it checks the subsequent character(s), and interprets the sequence as instructions to insert a specific character described by the sequence. 
 
For example, since string constants are delimited by double quote characters, if you want to embed a double quote character in a string you can't just type the double quote character, because that character will be taken to signify the end of the string. Instead, you use the escape sequence '\"', as in:
"This \"string\" has double quotes around the word string."
 
That string will print as:
This "string" has double quotes around the word string.
 
Other escape sequences enable you to insert other characters that are otherwise difficult to insert (or to see and identify when reading the source code):
 
\n     inserts a newline character
\t      inserts a tab character
\b     inserts a backspace character
\f      inserts a form feed character
\r      inserts a carriage return character
\"      inserts a " (double quote) character
\'      inserts a ' (single quote) character
\\      inserts a \ (back slash) character
\uDDDD  can be used to insert any character from the Unicode character set, by specifying the Unicode as four hex digits
 
Given this, one might think that to embed a line break in a string, one should use "\n" to insert a newline character. In fact, on platforms running the Unix operating system or any of its derivatives (such as Lynux), as well as on some other platforms, this will actually work. However, Java programs are intended to be platform independent, meaning that they run equally well on any platform that supports the Java Virtual Machine.
 
If you use the "\n" escape sequence to insert a newline character, this may not work on some platforms. In particular, on any of the DOS/Windows family of operating systems, this will not have the intended effect. This is because this platform requires that line breaks are represented by a pair of characters: carriage return followed by linefeed.
 
This is an historical artifact stemming from the fact that early computer systems used teletype terminals as input/output devices. The carriage return character caused the carriage, the device that actually struck the characters onto the paper, to return to the left margin, and the linefeed character fed the paper up one line, so that subsequent characters were printed on the next line of the page. Outputting only a return character would cause the next line of output to overprint the existing line, because the paper was not fed forward, and outputting only the linefeed character would cause the next line of characters to print on the next line, but starting at the point just after the position where the last character was printed on the previous line.
 
The DOS operating system kept the convention that line breaks are represented by the carriage return/linefeed pair (often denoted at CRLF), and the Windows operating system was based on DOS, and also maintained that convention.
 
Because different platforms use different conventions for how line breaks should be represented, you shouldn't directly insert the character sequence required by any particular platform, because that would make your program platform-specific: it would run correctly only on that platform and others that employed the same convention.
 
To get around this, you should use the system constant: System.line.separator. This constant will be bound to the appropriate sequence for the platform that the program is running on, and so the program will work equally well on all platforms. On Unix systems, this constant is bound to "\n", and on Windows systems it is bound to "\r\n".
 
String sep = System.line.separator;
String text = "This is line one." + sep + "This is line two.";

The code fragment above will create a string in the variable 'text' that when written to an output stream will print as:
 
This is line one.
This is line two.
 
 
 

Woodbridge Java tutors