开发者

Java: Are chars " and \" the same?

开发者 https://www.devze.com 2023-02-06 23:30 出处:网络
Given: char x = \'\"\'; char y = \'\\\"\';开发者_如何学Python Are x and y equal?They are equal. (the link executes the following test)

Given:

char x = '"';
char y = '\"';开发者_如何学Python

Are x and y equal?


They are equal. (the link executes the following test)

class foo{
    public static void main(String[] a){
       System.out.println('"' == '\"'); // prints true
    }
}


I didn't feel like running code so I just googled...

JLS 3.10.6 Escape Sequences for Character and String Literals

...and if your javac doesn't conform then it's broken and not my fault :)

The keywords I used were "JLS" (for Java Language Specification) and "character literals" (because that's what '?' is).


Yes.

class Test {
    public static void main (String[] args) {
        char x = '"';
        char y = '\"';
        boolean equal = (x == y);
        System.out.println("x == y ?" + equal);
    }
}

The output is true. Seems pretty conclusive.

0

精彩评论

暂无评论...
验证码 换一张
取 消