javavisual-studio-codecmd

Escape Sequence of bell alert not working in java


I am using escape sequence (JAVA) "\a" or '\a', then it simply gives me errors...

My code...

class Bell{
    public static void main(String args[]){
        System.out.println("\a");
    }
}

I don't know why it keeps on saying:

Bell.java:4: error: illegal escape character
        System.out.println("\a");
                             ^
1 error

When I use '\a' instead of "\a", again it shows me error:

Bell.java:4: error: illegal escape character
        System.out.println('\a');
                             ^
Bell.java:4: error: unclosed character literal
        System.out.println('\a');
                           ^
Bell.java:4: error: unclosed character literal
        System.out.println('\a');
                              ^
3 errors

Occurs in both VS Code and CMD but it gives error. Can anyone tell me what's going wrong?

My JDK version is 15.0.1


Solution

  • There is no such escape sequence in Java.

    See the JLS section 3.10.6 for which escape sequences are valid. There is no "single-character-after-backslash" escape sequence for the bell character.

    If you want to represent U+0007 in a string or character literal, use \u0007.