Escape Sequences and Comments

Estimated reading: 3 minutes 782 Views


In Java, comments are non-executable statements that are ignored by the compiler. They are used to provide explanations, documentation, and annotations within the code. There are three types of comments in Java:

  1. Single-line comments: Single-line comments begin with two forward slashes (//). Everything after // on the same line is considered a comment and is ignored by the compiler. Single-line comments are typically used for short explanations or comments on a single line of code.

    // This is a single-line comment
  2. Multi-line comments: Multi-line comments, also known as block comments, are enclosed between /* and */. Everything between these symbols, including newlines, is considered a comment. Multi-line comments are often used for longer explanations or comments that span multiple lines of code.

    /* This is a
       multi-line comment */
  3. Javadoc comments: Javadoc comments are a special type of comment used to generate documentation. They start with /** and end with */. Javadoc comments are used to describe classes, methods, and fields, and can include special tags to provide additional information such as parameters, return values, and exceptions.

     * This is a Javadoc comment for the MyClass class.
    public class MyClass {
         * This is a Javadoc comment for the myMethod method.
         * @param x This is a parameter of the method.
         * @return This method returns a value.
        public int myMethod(int x) {
            return x * x;

Escape Sequences:

In Java, escape sequences are special character combinations used to represent characters that are difficult or impossible to represent directly in a string literal. Escape sequences are preceded by a backslash () character. Here are some common escape sequences used in Java:

  1. \n: Represents a newline character. When used within a string literal, it moves the cursor to the beginning of the next line.
  2. \t: Represents a tab character. When used within a string literal, it inserts a horizontal tab.
  3. \’: Represents a single quote character. Used to include single quote within a string literal.
  4. \”: Represents a double quote character. Used to include double quotes within a string literal.
  5. \\: Represents a backslash character. Used to include a backslash within a string literal.
  6. \r: Represents a carriage return character.
  7. \b: Represents a backspace character.
  8. \f: Represents a form feed character.


public class EscapeSequencesExample {
    public static void main(String[] args) {
        // Newline (\n) escape sequence
        System.out.println("Hello\nWorld"); // Output: Hello
                                            //         World
        // Tab (\t) escape sequence
        System.out.println("Java\tProgramming"); // Output: Java    Programming
        // Double quote (\") escape sequence
        System.out.println("She said, \"Hello!\""); // Output: She said, "Hello!"
        // Backslash (\\) escape sequence
        System.out.println("C:\\Users\\John\\Documents"); // Output: C:\Users\John\Documents
        // Carriage return (\r) escape sequence
        System.out.println("Overwritten text\r123"); // Output: 123written text
        // Backspace (\b) escape sequence
        System.out.println("Back\bspace"); // Output: Backspace
        // Form feed (\f) escape sequence
        System.out.println("Hello\fWorld"); // Output: Hello
                                             //         World



Share this

Escape Sequences and Comments

Or copy link