int does not override Integer in Java

后端 未结 6 890
春和景丽
春和景丽 2020-12-11 02:23

I had a function in Java with method signature

public void myMethod (int someInt, String someString) 

in my abstract class and I had over-

相关标签:
6条回答
  • 2020-12-11 02:59

    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.....

    0 讨论(0)
  • 2020-12-11 03:05

    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.

    0 讨论(0)
  • 2020-12-11 03:16

    The reason the override doesn't work is because Integerand 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."

    0 讨论(0)
  • 2020-12-11 03:18

    No, these two signatures define two different methods.

    0 讨论(0)
  • 2020-12-11 03:21

    int and Integer are two different types in JAVA.Although autoboxing specifies the distinction at the source code level, but does not change the eternal fact that they are in fact two very different types.

    0 讨论(0)
  • 2020-12-11 03:23

    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, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

    See also

    • Java Language Guide/Autoboxing

    Related questions

    • What is the difference between an int and an Integer in Java/C#?
    • Is it guaranteed that new Integer(i) == i in Java? (YES!!! The box is unboxed, not other way around!)
    • Why does int num = Integer.getInteger("123") throw NullPointerException? (!!!)
    • Why null == 0 throws NullPointerException in Java?
    0 讨论(0)
提交回复
热议问题