• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why immutable types in Generic declarations?  RSS feed

 
Tom Brodhead
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a generic class, like this:



When I declare an object using the generic, why am I restricted to using immutable types as keywords in the declaration? For example, I can't declare:



Instead, (apparently, unless I'm mistaken) I must use:



Why is this? Are immutable types generic in a sense, too?

Many thanks in advance!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't limited to using immutable types. You could perfectly well use Date, for example, or ArrayList, or any of an unlimited number of types.

You just can't use primitive types, that's the only restriction.
 
Tom Brodhead
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't I use primitive types? What's the reasoning?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably they figured it would require more complicated rules, with little real benefit. You can convert an int to an Integer pretty easily, after all. If Java were designed today, I suspect it wouldn't even have primitives - they just complicate the rules. Back when it was designed, primitives were included because they were capable of giving faster execution times. I suppose that's still true, but the difference has shrunk to be too small to worry about most of the time.

Anyway, wrapper classes like Integer were originally created specifically to allow you to treat numbers as Objects, for anything primitives can't do. Like adding a number to a List. You can't do that with an int, only an Integer. When they later added generics, they continued the same pattern - support it for the wrapper classes, and let people use the wrapper classes whenever they need to talk about numbers.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for using immutable types as the Key in a Map or as elements in a HashSet is that they cannot change their hash codes. If you use a mutable type in those situations, and it changes its state, it will be impossible to find the original object again.
[campbell@campbell1110 java]$ java WrongSetDemo "Mr Campbell Ritchie" "Campbell Ritchie"
The set now contains Person: Campbell Ritchie:
I seems to have missed out part of the output. You will have to try and see for yourself what happens.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I think Tom actually meant primitive types instead of immutable types.

But ofcourse you're right that you should only use immutable types for keys in a HashMap or values in a HashSet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!