File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes return type of TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 113




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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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: 8430

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.


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

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

Joined: Aug 16, 2005
Posts: 12907
    
    3

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: 5787
    
    5

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: return type of TreeMap
 
Similar Threads
Recursive method to print prime factorization of a number
Incompatible types
Percentage issue
Incompatible operand types Scanner and int
Project Euler problem 6 algorithm