English 中文(简体)
in的内乱并不凌驾于内乱
原标题:int does not override Integer in Java
  • 时间:2010-08-02 08:09:59
  •  标签:
  • java

我在 Java有一套方法签名的功能。

public void myMethod (int someInt, String someString) 

在我的抽象类别中,我用方法掩盖了它。

public void myMethod (Integer someInt, String someString)

over不可行。 这是否自相矛盾? 我认为,自动箱也适用于超负荷使用的方法。

最佳回答

<代码>int和Integer两种不同类型。 在源代码一级为方案设计人的方便而进行分类,但并不改变事实上是两个 × 不同类型。

因此,你不能<代码>@ Override a methods which take an int with one that take an Integer <>/code>,反之亦然。

请注意,在宣布采用<代码>的方法之前,您可能应两次思考。 Integer und a int. 页: 1 Effective Java 2nd Edition, 项目49: Prefer primitives to tened primitives:

简言之,每当你选择时,先使用价格,而不是盒式价格。 主要类型比较简单、更快。 如果你必须使用盒式价格,就会小心! 自动箱子减少了使用盒式排出物的ver,但并非危险。 当贵方案将两个盒式的原始产品与=的操作者进行比较时,它进行身份比较,这几乎肯定不是你想要的。 如果贵方案采用混合式计算方法,涉及箱式和未箱式价格,则不设箱,如果你的方案没有箱子,则可以投掷<条码>。 最后,如果贵方案箱的原始价值,就可能产生昂贵和不必要的物体。

在有些地方,你别无选择,只能使用盒式放大器,例如通用物,但否则,你应当认真考虑,是否有理由决定使用盒式放大剂。

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

例如,你有三种方法。

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

以及当你援引时:

int a = 5;
add(a);

将援引方法1。

压倒性工作的原因是:Integer<>/code>和int有两种不同之处。 <代码>Integer为物体,int为原始类型。 Java对你来说是暗中进行的。 例如:

int myInteger = new Integer(5);

定型号为myInteger,价值为5。 正如Javadoc所说,

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

您认为,这一设想不会奏效,因为 Java提供自动箱的功能,因此,在运行时间,JRV不能决定哪一种方法可以打电话,因为这两种方法都适合论证类型。 因此,我认为这会造成错误,或者随意选择这两种方法。 只是看......。

急性呼吸道感染和肿瘤是两种不同的类型。 虽然汽车箱具体指明了源代码一级的区别,但并不改变其事实上是两种截然不同的永恒事实。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签