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

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?