Learn Java the Hard Way (Second Edition)

Exercise 4: Escape Sequences and Comments

Have you thought about what might happen if we wanted to display a quotation mark on the screen? Since everything we want to display is contained between quotation marks in the println() statement, putting a quote inside the quotes would be a problem.

Most programming languages allow for “escape sequences”, where you signal with some sort of escape character that the next character you see shouldn’t be handled in the normal way.

The following code demonstrates many of Java’s escape sequences.
Call it EscapeSequencesComments.java.

EscapeSequencesComments.java
 1 public class EscapeSequencesComments {
 2     public static void main( String[] args ) {
 3         // This exercise demonstrates escape sequences & comments (like these)!
 4         System.out.print( "Learn\tJava\n\tthe\nHard\tWay\n\n" );
 5         System.out.print( "\tLearn Java the \"Hard\" Way!\n" );
 6         // System.out.frimp( "Learn Java the Hard Way" );
 7         System.out.print( "Hello\n" ); // This line prints Hello.
 8         System.out.print( "Jello\by\n" ); // This line prints Jelly.
 9         /* The quick brown fox jumped over a lazy dog.
10            Quick wafting zephyrs vex bold Jim. */
11         System.out /* testing */ .println( "Hard to believe, eh?" );
12         System.out.println( "Surprised? /* abcde */ Or what did you expect?" );
13         System.out.println( "\\ // -=- \\ //" );
14         System.out.println( "\\\\ \\\\\\ \\\\\\\\" ); // it takes 2 to make 1
15         System.out.print( "I hope you understand \"escape sequences\" now.\n" );
16         // and comments. :)
17     }
18 }

When you run it, this is what you should see.

Java’s escape character is a backslash (“\”), which is the same key you press to make a pipe (“|”) show up but without holding Shift. All escape sequences in Java must be somewhere inside a set of quotes.

\" represents a quotation mark.

\t is a tab; it is the same as if you pressed the Tab key while typing your code. In most terminals, a tab will move the cursor enough to get to the next multiple of 8 columns.

It probably seems more complicated now because you’ve never seen it before, but when you’re reading someone else’s code a \t inside the quotes is less ambiguous than a bunch of blank spaces that might be spaces or might be a tab. Personally, I never ever press the Tab key inside quotation marks.

\n is a newline. When printing it will cause the output to move down to the beginning of the next line before continuing printing.

\\ is how you display a backslash.

On line 3 you will notice that the line begins with two slashes (or “forward slashes”, if you insist). This marks the line as a “comment”, which is in the program for the human programmers’ benefit. Comments are totally ignored by the computer.

In fact, as shown in lines 7 and 8, the two slashes to mark a comment don’t have to be at the beginning of the line; we could write something like this:

…and it would totally work. Everything from the two slashes to the end of that line is ignored by the compiler.

Line 8 does something funny. \b is the escape sequence for “backspace”, so it displays “Jello”, then emits a backspace character. That deletes the “o”, and then it displays a “y” (and then a \n to move to the next line).

Lines 9 and 10 are a block comment. Block comments begin with a /* (a slash then a star/asterisk) and end with an star and a slash (*/), whether they are on the same line or twenty lines later. Everything between is considered a comment and ignored by the compiler.

You can see a surprising example of a block comment in line 11. And on line 12 you can see that Strings (things in quotes) take precedence over block comments. Block comments are also sometimes called “C-style” comments, since the C programming language was the first one to use them.

Lines 13 and 14 demonstrate that to get a backslash to show up on the screen you must escape the backslash with another backslash. This matters when you’re trying to deal with Windows-style paths; trying to open "C:\Users\Graham_Mitchell\Desktop\foo.txt" won’t work in Java because the compiler will try to interpret “U” as something. You have to double the backslashes. ("C:\\Users\Graham_Mitchell\Desktop\\foo.txt")


“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell