• 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

Doubt in generics

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList sampleList = new ArrayList<Integer>();
ArrayList<Integer> sampleList = new ArrayList();

What is difference between above two statements. Are they type safe.

Can anybody explain the difference between upper bound and lower bound in generics in terms of adding elements and passing/returning values.

thanks
Rajeswari
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

1)ArrayList sampleList = new ArrayList<Integer>();

if you are not adding any thing to list ,the compiler will not giving any warrnings.

sampleList.add("appile");
it compiles ,but with warnings.
2)ArrayList<Integer> sampleList = new ArrayList<Integer>();

it is ok,it takes only integers.


What is your problem with upper bound and lower bound ?

Thanks
Anil Kumar
 
Rajeswari Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between adding type in reference and object creation

ArrayList sampleList = new ArrayList<Integer>();// type information is added in object creation
ArrayList<Integer> sampleList = new ArrayList(); // type information is added in reference

In upper bound(using super) we can add elements I think
But in lower bound(using extends)it is not so.

Please clarify this

thanks
Rajeswari
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
---------------------------------------------------------------
ArrayList sampleList = new ArrayList<Integer>();// type information is added in object creation
--------------------------------------------------------------

here you can add not only integer ,but also Double,Float,Long,Boolean, what ever it may ,you can add that to the list.It compiles even it runs,but if you try to retrieve the values,then at run time what comes out ? think about this.Here sampleList is not specific to any one like Integer or Double etc.


Generics are for compile time check only.
-----------------------------------------------------------
2) ArrayList<Integer> sampleList = new ArrayList(); // type information is added in reference
-------------------------------------------------------------

Here you can add only integers because you have declared sampleList is of type Integer only and the compiler knows that one.If you try to add any other thing ,the compiler will give compile time error.


-----------------------------------------------------------------
In upper bound(using super) we can add elements I think
------------------------------------------------------------------

Yes we can add elements using super.

for example

class Animal{}
class Dog extends Animal{}

List<Animal> animals = new ArrayList<Animal>();

public void addAnimal(List<? super Dog> animals)

In the above, the list type can be

List<Object> animals or List<Animal> or List<Dog>

because Obect is a super type of all the classes so we can pass.And the runtime object is Animal in the heap.That why we can add.




--------------------------------------------------
But in lower bound(using extends)it is not so
----------------------------------------------
I think now you can understand why this will not work


Thanks
Anil Kumar
 
Rajeswari Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anil. Now i got it
 
reply
    Bookmark Topic Watch Topic
  • New Topic