1

Because of how generics are implemented in Java (eventually compiling down to objects and casts), what are the benefits of using them?

Should I still use them for the compile time type safety? I'm used to the significant speed boost of generics in C# and I'm curious if people who work with Java still frequently use generics despite their implementation?

3
  • 1
    @KingNestor - Michael Borgwardt below has inferred that you are talking about collections parameterized by a primitive type (which is not possible in Java). You don't actually mention this (i.e. primitives) anywhere in your question - could you edit it? The answer I supplied is entirely valid and correct and I'm a bit miffed it's been downvoted considering that I'm not sure how I was supposed to know you were talking about primitives when you don't actually mention them anywhere. Commented Sep 2, 2009 at 7:15
  • @oxbow_lakes, It isn't just collections of primitives that there is a performance difference. You were most likely downvoted because you stated that generics "have nothing to do with performance" in bold, when this isn't true of C# generics, which was the OPs point. And we aren't talking microseconds of performance, we are talking at least a 2x speed up when doing many operations. Not having the autoboxing / unboxing is huge. Commented Sep 2, 2009 at 13:37
  • 1
    @oxbow_lakes, it seems you simply saw "performance" and "java generics" and ignored the C# reference in the question. The OP stated that he wondered why Java programmers used generics other than type-safety in Java because they dont have a performance improvement like C# does over its non-generic counterparts (by keeping the type information and not performing casts). Granted, type-safety is still reason enough to use them but you seemed of mis-understood the question. Commented Sep 2, 2009 at 13:46

3 Answers 3

8

The compile time type safety is important enough that I believe it warrants using generic arraylists, so you should use them, IMO, as I don't generally see a good reason not to.

It helps to let other developers know what you are storing in there, and the auto boxing/unboxing is very useful if using primitives. It simplifies the coding you have to do, even though it is more work for the compiler.

Sign up to request clarification or add additional context in comments.

4 Comments

@James: You say it is more work for the compiler. But does it represents an overhead in the runtime?
Not really. The compiles produces more or less the same code you would by using class casts...
To compare java and .net generics you can read this: jprl.com/Blog/archive/development/2007/Aug-31.html. It may help to see what a .NET programmer is looking for when using generics, compared to what a Java programmer is doing.
@Oscar Reyes, read up on .NET Generics vs Java Generics. Java Generics throw away type information and resort to using Object and casts. They did this because they wanted them to work on existing VM's. However, .NET made breaking changes, and these changes result in a significant speed improvement when compared to Java Generics.
4

The benefits of using Java generics are purely that your code is less cluttered with casts, you are able to state your intentions at compile-time about what your collections can contain and have these intentions checked by the compiler.

Java generics have nothing whatsoever to do with performance. As James Black says, because in Java, generics are erased, there is no difference at runtime between Java code which uses parameterized types and Java code which uses raw types.

So yes, we still use them because they make our code clearer

EDIT. I seem to have been downvoted because I did not realize that you were talking about the ability in C# to have a collection of primitives (what with them not actually being mentioned anywhere in the question): e.g.

List<int>

If I really care about performance (i.e. squeezing every last microsecond from a Java program) then I'd use an int[] array and, yes, it's a shame that Java doesn't have primitive-collections. For most programs, I don't think that this is a real issue, however, so I'm happy to use List<Integer> in Java for its added convenience.

4 Comments

In Java, there is no difference, that's the OP's point. In C# there is, exactly because it reifies the generics rather than erasing the type. That means the runtime code doesnt have* the casts that the Java compiler has to insert. I doubt that it results in a "significant speed boost", though.
He's probably talking about primitives. The speed difference between a "real" int list and one based on autoboxing cound indeed be significant.
Er no. I was talking about Java generics. Everything I've written in this post is correct, so I'm not entirely sure why I've been downvoted
Ah, I see. The OP is talking about C#'s ability to have a List<int>. So I seem to have been penalised because the OP has not phrased their question correctly?
1

Yes, you should use them for the improved type safety. By using raw typed collections, you are following an old code path that was only kept around for compatibility.

Generics in Effective Java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.