How it is possible in Java for false to equal true

Viewed 1140

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!!!");
        }
    }
}
2 Answers

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 \u000a is a newline, \u007d is the character "}" and \u007b 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

Related