简体   繁体   中英

How it is possible in Java for false to equal true

In this program, how can false be equal to 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. 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

EDIT - I understood, it closing the main method after first line and the second line will be outside main as separate block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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