Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How it is possible in Java for false to equal true

Tags:

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!!!");         }     } } 
like image 409
user3022679 Avatar asked Dec 04 '13 19:12

user3022679


People also ask

Can you use == for boolean in Java?

Typically, you use == and != with primitives such as int and boolean, not with objects like String and Color. With objects, it is most common to use the equals() method to test if two objects represent the same value.

How does true and false work in Java?

In Java, there is a variable type for Boolean values: boolean user = true; So instead of typing int or double or string, you just type boolean (with a lower case "b"). After the name of you variable, you can assign a value of either true or false.

Can you return true or false in Java?

In Java, you must declare a method of the boolean type in order for it to return a boolean value. The boolean method will return the boolean value, true or false. You can either return the variable containing a boolean value or use conditional statements to decide the returned value.

Is 1 false or true in Java?

The same section also says: "The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false.


1 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.

like image 146
Ted Hopp Avatar answered Oct 11 '22 14:10

Ted Hopp