• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return type of TreeMap

 
thejwal pavithran
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I am trying to write this program using a TreeMap but i get an incompatible error at the lines pointed above.

I believe that the return type of tm.get(num) here is Integer as calling tm.get(num).getClass() prints out java.lang.Integer.

Please help me..
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type of tm.get() is Object, because you're not using generics.

Use generics to indicate what the type of keys and values in the TreeMap is. For example:
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to tell the compiler, that the map values are going to be Integer types.
Recommended reading http://docs.oracle.com/javase/tutorial/java/generics/

[edit]Beaten by Jesper But maybe the link I provided might be useful.
 
thejwal pavithran
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, thanks ..its working properly now..but could you please explain why calling getClass() method gives Integer as the datatype?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the actual object stored in the map is an Integer object.

If you don't tell the compiler what types of objects the map is allowed to contain, it cannot know that your intention was that it can contain only Integer objects.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejwal pavithran wrote:hey guys, thanks ..its working properly now..but could you please explain why calling getClass() method gives Integer as the datatype?




If we call X.getClass(), it will tell us that the class of that object is java.lang.Integer. However, the compiler doesn't know that. When it looks at how we're using X, it only looks at the type the reference is declared to be, which in this case is Object. The only thing the compiler looks at the type of the RHS for is to determine whether it's legal to assign it to the variable on the LHS. After that, it doesn't go, "Oh, X is declared as an Object, but I see it's pointing to an Integer, so I'll let you use it as an Integer."

Although the details are different, this concept is exactly what's happening in your case. It's the difference between the compile-time type of a reference and the runtime type of the object it points to. The compiler only cares about the first one, and in the general case, it's impossible for it to know the second one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic