是字符“\”与“”“相同?(反斜杠双引号vs only-doublequote) [英] Is the char literal '\"' the same as '"' ?(backslash-doublequote vs only-doublequote)
问题描述
在 char
文字'\'
和'之间有任何区别'
?
推荐答案
绝对没有区别。两个 char
是 ==
。
There is absolutely no difference. The two char
are ==
.
System.out.println('\"' == '"'); // prints "true"
严格来说,没有必要在 char
literal,但不会改变这个事实,即 \
表示双引号字符 \\
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
.
- JLS 3.10.6 Escape Sequences for Character and String Literals
我们也有类似的情况,例如 String
文字:
We 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.
- JLS 3.10.5 String Literals
字符串文字 - 或者更一般地说,是使用
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 $ c $中的单引号c> literal 必须转义
- 因为
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
- 去阅读最有用的
这篇关于是字符“\”与“”“相同?(反斜杠双引号vs only-doublequote)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Because
- 因为