简体   繁体   中英

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

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

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.

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. Dynamic proxies for example, Proxy.newProxy() returns Object type. 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;

while with the object example, that would be possible.

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