Escape Sequences in Java

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 11:- Escape Sequences in Java

In Java, escape sequences are special characters that are used to represent non-printable and special characters within a string. When you want to include characters that cannot be easily typed or have special meanings, you use escape sequences to represent them.

Escape sequences start with a backslash \ followed by a specific character. Here are some commonly used escape sequences in Java:

  1. \n: Newline - Inserts a new line at the specified position.
  2. \t: Tab - Inserts a horizontal tab at the specified position.
  3. \\: Backslash - Inserts a backslash character.
  4. \": Double Quote - Inserts a double quote within a string.
  5. \': Single Quote - Inserts a single quote within a character literal.
  6. \r: Carriage Return - Moves the cursor to the beginning of the line.
  7. \b: Backspace - Moves the cursor back one character (not commonly used in modern applications).
  8. \f: Form Feed - Advances the cursor to the next page or form.
  9. \uXXXX: Unicode Escape - Represents a Unicode character specified by the four hexadecimal digits (XXXX).

Here are some examples of using escape sequences in Java:

javaCopy code

public class EscapeSequenceExample {    public static void main(String[] args) {        // Newline        System.out.println("Hello,\nWorld!");        // Tab        System.out.println("Name\tAge");        // Backslash        System.out.println("C:\\Users\\username\\Desktop");        // Double Quote        System.out.println("He said, \"Hello!\"");        // Single Quote        char letter = '\'';        System.out.println("This is a " + letter);        // Carriage Return        System.out.println("Hello\rWorld");        // Form Feed        System.out.println("Page 1\fPage 2");        // Unicode Escape        System.out.println("\u00A9 All rights reserved.");    } }

Output:

vbnetCopy code

Hello, World! Name    Age C:\Users\username\Desktop He said, "Hello!" This is a ' Hello World Page 1      Page 2 © All rights reserved.

Escape sequences help in representing special characters and formatting text in various Java applications, especially when working with strings and character literals.

3. Input And Output Data Types

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support