Equal operator

Why is the condition being if

evaluated true

in this program? How is it 10

equivalent 10.0

?

 public class Test {
     public static void main(String[] args) {
         int i = 10;
         double d = 10.0;

         if (i == d) {
             System.out.println("hi");
         } else {
             System.out.println("bye");
         }
    }
}

      

+3


source to share


2 answers


Due to the binary digital advertising rules described in the Java Language Specification, section 5.6.2. These rules apply to binary operations on various types of numbers. It says that:



If one of the operands is of type double, the other is converted to double.

+11


source


int

will be converted to double

when we compare int to double. see https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21.1



+3


source







All Articles