简体   繁体   English

如何在Java中将false视为真等

[英]How it is possible in Java for false to equal true

In this program, how can false be equal to true: 在这个程序中,false如何等于true:

public class Wow {
    public static void main(String[] args) {
        if ( false == true ){ // \u000a\u007d\u007b
            System.out.println("How is it possible!!!");
        }
    }
}

Well, I'll be generous and assume that this question was asked out of innocence. 好吧,我会慷慨地认为这个问题是出于无罪而被问到的。

The Java compiler parses Unicode escape sequences very early in the process. Java编译器在此过程的早期解析Unicode转义序列。 In particular, it does this before stripping comments or checking for syntax. 特别是,它在剥离注释或检查语法之前执行此操作。 Since \ is a newline, \} is the character "}" and \{ is the character "{", the parser is actually parsing this program: 由于\ \}是换行符, \}是字符“}”而\{是字符“{”,解析器实际上正在解析此程序:

public class Wow{
    public static void main(String[] args) {
        if ( false == true ){ // 
}{
            System.out.println("How is it possible!!!");
        }
    }
}

This program will always print the "impossible" output. 该程序将始终打印“不可能”的输出。

I was just experimenting this question,(answer as well) , and found interesting behavior 我只是试验这个问题,(也回答),并发现了有趣的行为

public class TestUniCode {

    public static void main(String[] args) {
        System.out.println(" Printing first line");
        // \u000a\u007d\u007b
        System.out.println(" Printing second line");
    }
}

And very surprisingly (for me) it only prints Printing first line , and ignores the second line 而且非常令人惊讶(对我而言)它只打印Printing first line ,而忽略了第二行

EDIT - I understood, it closing the main method after first line and the second line will be outside main as separate block 编辑 - 我明白了,它在第一行之后关闭主要方法,第二行将作为单独的块在main之外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM