my dog learned polymorphism*
The moose likes Beginning Java and the fly likes return type of TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return type of TreeMap" Watch "return type of TreeMap" New topic
Author

return type of TreeMap

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118




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..

on job hunt
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118
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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
wood burning stoves
 
subject: return type of TreeMap