Is there\'s any difference between char
literals \'\\\"\'
and \'\"\'
?
There is absolutely no difference. The two char
are ==
.
System.out.println('\"' == '"'); // prints "true"
Strictly speaking it's not necessary to escape a double quote in a char
literal, but it doesn't change this fact that \"
denotes the double quote character \u0022
.
String
analogWe also have the analogous situation for String
literals:
System.out.println("\'".equals("'")); // prints "true"
In fact, we can even go a step further and use ==
for reference equality:
System.out.println("\'" == "'"); // prints "true"
The second snippet proves that the two string literals are really equal, and therefore subject to string interning at compile-time.
String literals --or, more generally, strings that are the values of constant expressions-- are "interned" so as to share unique instances, using the method
String.intern
.
char
literal MUST be escaped
char
literal is quoted in single-quotesString
literal MUST be escaped
String
literal is quoted in double-quotes