• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java primitive types generics backward compatibility on examples  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trying to understand what "to maintain backwards compatibility with previous JVM runtimes." means in explanations why java doesn't support primitive type generics:
Gen<int> inums = new Gen<int>(100);

an example of such explanation:
http://stackoverflow.com/questions/2721546/why-dont-generics-support-primitive-types

Can anyone provide the explanation how exactly adding such functionality would break old code?
Why a new JVMs can't support both old code and new one?
if it see old code it could execute it using old part of it
if it see new code it could execute it using new part of it
Where is the problem?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection classes, such as ArrayList, can only store objects. Primitive types are not objects.

As the StackOverflow answer explains, generics in Java are entirely a compile-time thing - the compiler uses them to check that you use the types correctly, but the actual byte code that's produced by the compiler doesn't know anything about the generic types. That process is called type erasure. The reason why generics are implemented with type erasure in Java was mainly for backward compatibility. That's also why raw types still exist in Java.

It means that an ArrayList<Something> will look like a plain, raw ArrayList at runtime - it's just a plain ArrayList that contains objects. Since primitive types are not objects, you can't store primitive types in such an ArrayList.

Ofcourse there are wrapper classes for each of the primitive types (int -> Integer, long -> Long, short -> Short, etc.) so in principle it would have been possible to make it so that an ArrayList<int> would use auto-boxing to store ints in Integer objects, which could be stored in an ArrayList. But the designers of the Java language didn't decide to take auto-boxing that far.
 
Max Konstantinov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Collection classes, such as ArrayList, can only store objects. Primitive types are not objects.

As the StackOverflow answer explains, generics in Java are entirely a compile-time thing - the compiler uses them to check that you use the types correctly, but the actual byte code that's produced by the compiler doesn't know anything about the generic types. That process is called type erasure. The reason why generics are implemented with type erasure in Java was mainly for backward compatibility. That's also why raw types still exist in Java.

It means that an ArrayList<Something> will look like a plain, raw ArrayList at runtime - it's just a plain ArrayList that contains objects. Since primitive types are not objects, you can't store primitive types in such an ArrayList.

Ofcourse there are wrapper classes for each of the primitive types (int -> Integer, long -> Long, short -> Short, etc.) so in principle it would have been possible to make it so that an ArrayList<int> would use auto-boxing to store ints in Integer objects, which could be stored in an ArrayList. But the designers of the Java language didn't decide to take auto-boxing that far.

1) I take JVM 1.7 add functionality allowing this ArrayList<int>
2) If I see primitive type inside <> I don't do erasure and create true primitive type ArrayList
What's the problem with that?
Why do we need erasure?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!