Free Java course. Sign Up for tracking progress →

Java: Escaping sequences

We want to show the dialogue of the Mother of Dragons with her child:

- Are you hungry?
- Aaaarrrgh!

If you display a line with the following text:

System.out.print("— Are you hungry?— Aaaarrrgh!");

it will turn out like this:

— Are you hungry?— Aaaarrrgh!

We need to somehow tell the interpreter "click on enter" — to make a line break after the question mark.

In Java, \n is a line break:

System.out.print("— Are you hungry?\n— Aaaarrrgh!");

result:

— Are you hungry?
— Aaaarrrgh!

\n is an example of the escape sequence (escape sequence). They are also called control structures.

While typing in some Word, you press Enter at the end of the line. The editor puts a special invisible character at the end of the line, which is called LINE FEED (LF). In some editors, you can even turn on the display of invisible characters. Then the text will look something like this:

— Hello!¶
— Oh, hi!¶
— How are you?

A device that outputs the corresponding text takes this symbol into account. For example, the printer, when meeting with LF, pulls the paper up one line, and the text editor transfers all subsequent text below, also one line.

Although there are more than a dozen of such characters, there are often only a few in programming. In addition to line breaks, these characters include tabulation (a break, obtained by pressing the Tab button) and carriage returnValue (only in Windows). We, programmers, often need to use, for example, the translation of the string \n to properly format the text.

System.out.print("Gregor Clegane\nDunsen\nPolliver\nChiswyck");

Attention! Escaping sequences like \n work only inside double quotes!

The screen will display:

Gregor Clegane
Dunsen
Polliver
Chiswyck

For convenience, there is a method System.out.println, which allows you to display some value on the console and then transfer the console to the next line. For example:

System.out.println("Hello");
System.out.println("World");

The screen will display:

Hello
World

Pay attention to the following points:

1. It does not matter what comes before or after \n: a character or an empty string. The transfer will be detected and executed in any case.

2. Remember that a string can contain only one character or zero characters at all? And the line can contain only \n:

System.out.print("Gregor Clegane");
System.out.print("\n");
System.out.print("Dunsen");

Here we print one line with the name, then one line “line feed”, and then another line. The program will display:

Gregor Clegane
Dunsen

3. Despite the fact that in the source code of a program, a sequence of type \n looks like two characters, from the point of view of the interpreter it is a special single character.

4. If we need to display \n exactly as text (two separate printable characters), then we can use the already known screening method, adding one more \ at the beginning. That is, the sequence \\n is displayed as the characters \ and n following each other.

System.out.print("Joffrey loves using \\n");

the screen will be released:

Joffrey loves using \n

A small but important note about Windows. On Windows, \r\n is used to translate strings by default. Such a combination works well only in Windows, but creates problems when transferring to other systems (for example, when the development team has both Windows and Linux users). The fact is that the sequence \r\n has a different interpretation depending on the selected encoding (discussed later). For this reason, in the development environment, it is customary to always use \n without \r, since LF is always treated the same way and works fine in any system. Remember to configure your editor to use \n.

Instructions

Write a program that displays:

— Did Joffrey agree?
— He did. He also said "I love using \n".

The program uses only one println, but the result on the screen should look exactly like the one shown above.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips


If you got stuck and don't know what to do, you can ask a question in our community