是字符'\“与“”相同“ ?(backslash-doublequote vs only-doublequote) [英] Is the char literal '\"' the same as '"' ?(backslash-doublequote vs only-doublequote)
问题描述
char
文字'\'
和'之间有任何区别'
Is there's any difference between char
literals '\"'
and '"'
?
推荐答案
没有什么区别。两个 char
是 ==
。
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
.
严格来说,不必在 char
literal,但它不改变这个事实, \
表示双引号字符 \\ u0022
。
- JLS 3.10.6 Escape Sequences for Character and String Literals
We also have the analogous situation for String
literals:
我们也有 String
文字的类似情况:
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.
第二个片段证明两个字符串文字真的相等,因此subject
- JLS 3.10.5字符串字面值
- JLS 3.10.5 String Literals
字符串文字 - 或者更常见的是常量表达式的字符串 - 是interned,以便共享唯一的实例,使用方法
String.intern
。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
. - How do I compare strings in Java?
- Java
String.equals
versus==
- Where do Java and .NET string literals reside?
- When
"" == s
isfalse
but"".equals( s )
istrue
-
char
literal MUST 被转义
- 因为
char
单引号
- A single-quote in a
char
literal MUST be escaped- Because
char
literal is quoted in single-quotes
- 因为
String
文字在双引号中引用
- Because
String
literal is quoted in double-quotes
- 以最容易阅读的方式阅读
这篇关于是字符'\“与“”相同“ ?(backslash-doublequote vs only-doublequote)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Because
- 因为