tags:

views:

241

answers:

3

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?

+8  A: 

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.

James Black
@James: You say it is more work for the compiler. But does it represents an overhead in the runtime?
OscarRyz
Not really. The compiles produces more or less the same code you would by using class casts...
Savvas Dalkitsis
I wonder what is KingNestor talking about then ... :)
OscarRyz
To compare java and .net generics you can read this: http://www.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.
James Black
@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.
Simucal
+4  A: 

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.

oxbow_lakes
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 *doesn*t have* the casts that the Java compiler has to insert. I doubt that it results in a "significant speed boost", though.
Michael Borgwardt
He's probably talking about primitives. The speed difference between a "real" int list and one based on autoboxing cound indeed be significant.
Michael Borgwardt
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
oxbow_lakes
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?
oxbow_lakes
+1  A: 

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

Wayne Young