This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Difference in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference in "? extends Object" and "Object" in Generics" Watch "Difference in "? extends Object" and "Object" in Generics" New topic
Author

Difference in "? extends Object" and "Object" in Generics

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hello,
I want to create a HashMap that would hold String keys and values of type [Integer,Date,String.Boolean,Double].
I did

This gave me error while trying to put the different types of Object into the Map as value.
When I reverted back to the code below, everything compiled fine.

I could not relate Generic fundamentals I have read to this difference.
AFAIK instead of talking in terms of Base Object class , that might give ClassCastException at later point of time, its better to use Generic types.

Thanks in Advance

Regards,
Amit


Regards,
Amit
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Java Generics - Wildcards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

With Map<String,? extends Object>, you don't know the actual type - it may be Map<String,Object>, but also Map<String,String> or Map<String,Integer>. Therefore, you cannot put anything into the map. With Map<String,Object>, you know the actual type is Object so you can put any object into the map.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

amit punekar wrote:Hello,
I want to create a HashMap that would hold String keys and values of type [Integer,Date,String.Boolean,Double].
I did

This gave me error while trying to put the different types of Object into the Map as value.
When I reverted back to the code below, everything compiled fine.

I could not relate Generic fundamentals I have read to this difference.
AFAIK instead of talking in terms of Base Object class , that might give ClassCastException at later point of time, its better to use Generic types.

Thanks in Advance

Regards,
Amit




by default every API classes are children of Parent- Object class so for any of Wrapper classes you need not no extend that way.
else, better to use


so that you can convert the needed values also.

Dhruva
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
amit punekar wrote:



Also, AFAIK, you cannot have a wildcard notation in object creation i.e.



will throw compile time error. but not this:



And if you are using wildcard in your references like in your case, you will not be able to *add* to the collection.
This is what I had read in SCJP Sun Certified Programmer for Java5 Study Guide.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi
@Rob,
Thanks for the explanation.
It does explain me what I understood wrong about wild-cards. But then I really did not understand in which case I can use "? extends Object" .
@amitabh,
The code compiles fine. But when you try to put Integer,String of different Java Wrapper classes inside it, compiler does not allow as Rob explained.
@Ryan.
I had gone through the Generics tutorial and hence thought of using the "? extends Object". It was my mistake that what I understood from tutorial could not apply to the scenario I was handling.

thanks to all once again for your kind help.

Regards,
Amit

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference in "? extends Object" and "Object" in Generics
 
Similar Threads
Struggling with generics, "unchecked call to put()"
Help with Java Generics...
HashMap
Generics : Passing a Map of <String, List<something extending a superclass>>
Java Generic question