Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

return type of TreeMap  RSS feed

 
Ranch Hand
Posts: 146
  • 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..
 
Java Cowboy
Sheriff
Posts: 16084
88
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:
 
Bartender
Posts: 11445
18
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.
 
sinatra roger
Ranch Hand
Posts: 146
  • 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
Sheriff
Posts: 16084
88
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.
 
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
Boost this thread!