0

In Java how does the == operator internally work, I'm not able to analyze

What is the output for the below code

public class Demo {
    public static void main(String[] args) {
        
        System.out.println(0.1 * 2 == 0.2);
        
        System.out.println(0.1 * 3 == 0.3);}
}

When I execute this code, I am getting : true false output. How it comes I'm not able to understand. How does JVM calculate the operations?

phuclv
  • 37,963
  • 15
  • 156
  • 475
S. Balaji
  • 1
  • 1

1 Answers1

-3

In == operator it checks whether the first value is similar to the second value. If yes it will print True (the statement is True), if not it will print False (The statement is False)

System.out.println(0.1 * 2 == 0.2);

In this statement it is asking whether 0.2 is equal to 0.2. Since it is correct it will print True.

  • Yes your right, i am agree with your answer but in second case : 0.1 * 3 == 0.3 , it is also true , it should give output true, why it's giving output as a false – S. Balaji Oct 24 '20 at 05:45