In the previous lesson, you learned about the print() and println() methods. However, there may be some restrictions towards characters you might want to include in your output. For example, if you wanted to actually print double quotes you may use the following code.

public class Main{
    public static void main(String[] args) {
        System.out.println(""Hello, world!"");
    }
}

This code will fail and will not work as the inner double quotes will cause an error. The reason for this is that the double quote symbol is used by java as an indicator for a message (formerly known as String). In this example, there is no way for java to know the difference between a double quote used for the message and the double quote used as a character to be printed.

Printing a Double Quote

Printing a double quote is still feasible in java. All you have to do is write a backward slash (\) before every double quote that needs to be printed. The new code will be:

public class Main{
    public static void main(String[] args) {
        System.out.println("\"Hello, world!\"");
    }
}

The slash (\) character will tell java to skip the double quotes, i.e: these double quotes are to be treated as a character, hence they are to be printed.

Escape Characters

Escape characters (or escape sequence) are characters that are preceded by a backslash (\). The escape characters have a special meaning in java and must be enclosed in double-quotes. In the previous section you already used an example of escape character (\”) to print the double-quotes. There are a total of 8 escape characters that are listed in Table 1.

Escape CharactersDescription
\tIt is used to insert a tab in the text at this point.
\’It is used to insert a single quote character in the text at this point.
\”It is used to insert a double quote character in the text at this point.
\rIt is used to insert a carriage return in the text at this point.
\\It is used to insert a backslash character in the text at this point.
\nIt is used to insert a new line in the text at this point.
\fIt is used to insert a form feed in the text at this point.
\bIt is used to insert a backspace in the text at this point.
Table 1 – list of escape characters in java

Here are some examples and their output.

public class Main{
    public static void main(String[] args) {
        System.out.println("\"I am enclosed in double quotes.\"");
    }
}
public class Main{
    public static void main(String[] args) {
        System.out.println("I am\non a new line.");
    }
}
public class Main{
    public static void main(String[] args) {
        System.out.println("I am\ta tab space.");
    }
}
public class Main{
    public static void main(String[] args) {
        System.out.println("I am\\a backslash.");
    }
}

Other Lessons in Introduction to Programming in Java

No. Lesson Reading Time
1 Introduction to Java 5 mins
2 Setting up the environment in Java 5 mins
3 Java Hello World Program 5 mins
4 Java Output 5 mins
5 Java Escape Characters 5 mins
6 Variables & Types 5 mins
7 Arithmetic Operators in Java 5 mins

Related Subjects

or view all subjects
No realted subjects found.