• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

questions on generics

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
umesh rawat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody there to answer
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one is obligated to answer your question. Do you not think that expecting an answer in under three hours, especially on a Saturday afternoon is a tad bit pushy?

Please read this and try to show some patience.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well umesh,

The first question has no line number 10, so I can't explain it to you.

2.
This program returns null because when you assign a new HashTest object to obj1, the HashMap is not effected. So when you try to search for a key which has a object of class HashTest with the value 6, it is not found, this is why null is returned.

3, 4 and 5. you just have to remember that you can assign a non-typed collection class to a typed one. This is for backward compatibility as you might use some old code which doesn't have generics.

But you cannot assign a typed collection to another typed collection.

ArrayList<Object> hs = new ArrayList<String>();

This wont compile. This is because generics are meant for type safety and doing this will make the code unsafe.. Now you will say that how will the code be unsafe. This is how

hs.add(new Integer());

here we are adding an Integer to a String list. This is possible because we are using a list which has a reference which has a type Object. But actually the list is of type String. Now I don't want to confuse you much here. If you want you can read about generics from sun generics tutorial, as you seem to be confused..
reply
    Bookmark Topic Watch Topic
  • New Topic