Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile time error while adding unicode \u0022

I'm trying to add double quotes in a string using the unicode, but when I do so i get the compilation time error.

String unicCode = "\u0022"; //This line gives a compile time error.

The compilation error is that I get is

String literal is not properly closed by a double-quote.

Could some one help me to understand what are the escape characters required to be used to append a unicode of a double quotation mark (").

like image 425
user3701861 Avatar asked Dec 26 '22 02:12

user3701861


1 Answers

 public static void main(String[] args) {
        String quote = "\u005c\u0022";
        System.out.println(quote);
    }

Output

"

public static void main(String[] args) {
        String quote = "\u005c\u0022" + "abc" + "\u005c\u0022";
        System.out.println(quote);
    }

Output

"abc"

If you wanted to put the two double quote chars into the string literal, you can do it with normal escape sequences. But you can't do with Unicode escapes because Java provides no special treatment for Unicode escapes within string literals.

String quote = "\"";

// We can't represent the same string with a single Unicode escape.

// \u0022 has exactly the same meaning to the compiler as ".

// The string below turns into """: an empty string followed

// by an unterminated string, which yields a compilation error.

String quote = "\u0022";
like image 159
Ankur Singhal Avatar answered Dec 28 '22 10:12

Ankur Singhal