简体   繁体   中英

int does not override Integer in Java

I had a function in Java with method signature

public void myMethod (int someInt, String someString) 

in my abstract class and I had over-ridden it with method

public void myMethod (Integer someInt, String someString)

The over ride does not work. Is this an inconsistency ? I thought auto-boxing applied to method signature over-ride as well.

int and Integer are two different types. Autoboxing blurs the distinction at the source code level for the convenience of programmers, but does not change the fact that they are in fact two very different types.

As such, you can not @Override a method that takes an int with one that takes an Integer and vice versa.

Note that you should probably think twice before declaring a method to take an Integer instead of an int . Here's an excerpt from Effective Java 2nd Edition, Item 49: Prefer primitives to boxed primitives :

In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw NullPointerException . Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

There are places where you have no choice but to use boxed primitives, eg generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

See also

Related questions

不,这两个签名定义了两种不同的方法。

They are absolutely not overridden but overload since the parameters are different. And JVM will choose the method to launch base on this: widen - boxing - var args...

For example, you have three methods

void add(long n) {} // call this method 1
void add(int... n) {} // call this method 2
void add(Integer n) {} // call this method 3

and when you invoke:

int a = 5;
add(a);

the method 1 will be invoked.

The reason the override doesn't work is because Integer and int are two different things. Integer is an object, whereas int is a primitive type. Java does the implicit typecasting for you. For example:

int myInteger = new Integer(5);

Will create a primitive int type called myInteger with a value of 5. As the Javadoc says,

"The Integer class wraps a value of the primitive type int in an object."

You are correct that this scenario will not work because Java provides the functionality of the autoboxing, so at runtime JVM can not decide which method to call because it can call both method as both method fits for the argument type. So I think it will give an error or will choose either method randomly..... Just run it and see.....

int和Integer是JAVA中的两种不同类型。虽然autoboxing指定了源代码级别的区别,但并没有改变它们实际上是两种非常不同类型的永恒事实。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM