How does this java code work?
•
Java
I have this code
class NumberMachine { public static void main(String [] args) { Integer wi1 = new Integer("420"); int i = 101; Integer wi2 = i*420/101; if(wi1 == wi2) System.out.print(" =="); if(wi1.equals(wi2)) System.out.print(" equal"); float f = 1.23f; new NumberMachine().printIt(f); } void printIt(Float f) { System.out.println(" Float"); } void printIt(double d) { System.out.println(" double"); } }
Why is the result "equal double" but not "equal floating"?
Solution
To determine which method should be called, the compiler will follow the following list, such as JLS #5.3 and JLS #15.12 2:
>Identity transformation (§ 5.1.1) > extended original transformation (section 5.1.2) = = > printit (double) works here > extended reference transformation (section 5.1.5) > a boxing transformation (§ 5.1.7) optionally followed by a widened reference transformation = = > if there is no printit (double), printit (float) > an unboxing transformation (section 5.1.8) will be called here, optionally followed by a widened primitive transformation
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码