是字符“\”与“”“相同?(反斜杠双引号vs only-doublequote) [英] Is the char literal '\"' the same as '"' ?(backslash-doublequote vs only-doublequote)

查看:164
本文介绍了是字符“\”与“”“相同?(反斜杠双引号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 is false but "".equals( s ) is true

  • char 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆