简体   繁体   English

在Java中,何时应该使用“Object o”而不是泛型?

[英]In Java, when should I use “Object o” instead of generics?

For example, I could write either of these: 例如,我可以写下以下任何一个:

class example <T>
{
    ...

    public void insert (T data)
    {
        ...
    }
}

or 要么

class example
{
    ...

    public void insert (Object o)
    {
        ...
    }
}

Is there a signficant difference between the 2 in terms of performance? 在性能方面,两者之间是否存在显着差异? With generics I could restrict the type of the parameter and with the second approach I guess it wouldn't be necessary to define the type of the object as it is created. 使用泛型我可以限制参数的类型,并且使用第二种方法,我想在创建对象时没有必要定义对象的类型。

Also, with the second approach I could basically insert anything into the class, right? 另外,使用第二种方法我基本上可以在课堂上插入任何内容,对吗? Whereas with generics every element in the class would be of the same type. 而对于泛型,类中的每个元素都是相同的类型。

Anything else I'm missing? 还有什么我想念的吗?

The only reason to write the latter is if you must target an earlier JVM. 编写后者的唯一原因是您必须定位较早的JVM。 Generics are implemented by type-erasure, so they have no runtime impact - only added compile time checking which will improve your code. 泛型是通过类型擦除实现的,因此它们没有运行时影响 - 只增加了编译时间检查,这将改善您的代码。

Of course if you need a collection which holds any old object, or a mix of several which don't have a common superclass, you need the plain Object variation (but then your class can still be generic and instantiated with new ...<Object>). 当然,如果你需要一个包含任何旧对象的集合,或者有几个没有共同超类的混合,你需要普通的Object变体(但是你的类仍然可以是通用的并用new ...实例化。对象>)。

I think you pretty much nailed it. 我认为你几乎已经钉了它。 There is no performance difference. 没有性能差异。 Generics are rationalized away ( Type Erasure ) when the code is compiled, and don't exist anymore at runtime. 在编译代码时,泛型被合理化( Type Erasure ),并且在运行时不再存在。 They just add casts when needed and do type-checking as you stated. 他们只需在需要时添加强制转换,并按照您的说明进行类型检查。 Neal Gafter wrote a nice overview of how they work, of the current problems with Generics and how they could be solved in the next version of Java: http://gafter.blogspot.com/2006/11/reified-generics-for-java.html Neal Gafter写了一篇关于它们如何工作的概述,Generics当前的问题以及如何在下一版Java中解决它们: http//gafter.blogspot.com/2006/11/reified-generics-for- java.html

There shouldn't be a performance difference. 不应该有性能差异。

However, Java does not offer parameter variance, so there are situations where you will be overriding pre-generics functions such as equals, compareTo, etc. where you will have to use Objects. 但是,Java不提供参数方差,因此在某些情况下,您将覆盖pre-generics函数,例如equals,compareTo等,您必须使用Objects。

Some of the encounters where I had to use 'Object' instead of Generics were those of compulsion than of a choice. 我不得不使用'对象'而不是泛型的一些遭遇是强迫而不是选择。 When working with pre-generic code or libraries built around pre-generic api, one has little choice. 使用预通用代码或围绕预通用api构建的库时,人们别无选择。 Dynamic proxies for example, Proxy.newProxy() returns Object type. 例如,动态代理,Proxy.newProxy()返回Object类型。 Passing generic context (where a context can be anything) is another instance. 传递泛型上下文(上下文可以是任何东西)是另一个实例。 Some of my friends argue that are as good as no-generics. 我的一些朋友认为这与非仿制药一样好。 As far as performance is concerned, there shouldn't be any overhead, considering type erasure. 就性能而言,考虑到类型擦除,不应有任何开销。

Regarding performance, i agree with the people above. 关于表现,我同意上面的人。

Regarding this point of yours 关于你的这一点

"Also, with the second approach I could basically insert anything into the class, right? Whereas with generics every element in the class would be of the same type." “另外,使用第二种方法我基本上可以在类中插入任何内容,对吧?对于泛型,类中的每个元素都是相同的类型。”

One more advantage of generics is there is a type check for assignment of the example instance itself. 泛型的另一个优点是有一个类型检查来分配示例实例本身。

Say for example you had an Example e1 of and another Example e2 of , type safety would be maintained and you would never be able to do e1=e2; 比如说你有一个示例e1和另一个示例e2,类型安全将被维护,你永远不能做e1 = e2;

while with the object example, that would be possible. 与对象示例一样,这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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