multiple-bounds

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

点点圈 提交于 2019-12-04 08:03:25
问题 I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. <T extends AutoCloseable & Cloneable> void printType(T... args) { System.out.println(args.getClass().getComponentType().getSimpleName()); } // printType() prints "AutoCloseable" It is clear to me that there is no type <T extends AutoCloseable & Cloneable> at runtime, so the compiler makes the least wrong thing it can do and creates an array with the type of one of the two

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

隐身守侯 提交于 2019-12-02 20:23:51
I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. <T extends AutoCloseable & Cloneable> void printType(T... args) { System.out.println(args.getClass().getComponentType().getSimpleName()); } // printType() prints "AutoCloseable" It is clear to me that there is no type <T extends AutoCloseable & Cloneable> at runtime, so the compiler makes the least wrong thing it can do and creates an array with the type of one of the two bounding interfaces, discarding the other one. Anyway, if the order of the interfaces is switched, the