Escape characters in Java

Escape characters in Java are special characters that are used to represent characters that cannot be entered directly into a string. They are preceded by a backslash (\) and are used to escape the meaning of certain characters, such as quotes, line breaks, and others.

Some common escape characters in Java include:

\n: Represents a new line. When used in a string, it creates a line break, like this:

String s = "Hello\nWorld!";

\t: Represents a horizontal tab. When used in a string, it creates a tab space, like this:

String s = "Hello\tWorld!";

\”: Represents a double quote. When used in a string, it allows you to include double quotes within the string, like this:

String s = "Hello \"World!\"";

\’: Represents a single quote. When used in a string, it allows you to include single quotes within the string, like this:

String s = "Hello 'World!'";

\\: Represents a backslash. When used in a string, it allows you to include a backslash within the string, like this:

String s = "Hello \\ World!";

In addition to these common escape characters, Java also provides a way to represent characters using their Unicode value. This is done using the \u escape sequence, followed by the four-digit hexadecimal value of the character, like this:

String s = "\u0048\u0065\u006c\u006c\u006f World!";

In conclusion, escape characters are an important part of Java’s string handling capabilities. They allow you to represent characters that cannot be entered directly into a string, and provide a way to include special characters, such as quotes and line breaks, within your strings. With a little practice, you’ll be able to use escape characters like a pro!

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in