Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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 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

Generics..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, this is my own code.

import java.util.*;
class Plant{

public static void main(String args[]){
List<String> li=new ArrayList();
li.add(new Object());//error
li.add(new String("c"));


}
}

Here we are not allowed to add anything other than String,why we are not calling this as type safe collection?

what is the difference between

List<String> li=new ArrayList();
and
List<String> li=new ArrayList<String>();?

please someone clear this..

thanks
Preetha
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if this has been discussed already...please someone give me the link.
 
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the type safe checking is made at compile time only so
List<String> li=new ArrayList();
and
List<String> li=new ArrayList<String>();

works the same way i.e.they allow only string to be added to the list.
Adding anything other than String will give a compile wrror in both cases.
What actually the li contains ,whether an arrayList() or ArrayList<String>()
is a runtime time issue and at Runtime due to type erasure ArrayList<String>()also becomes ArrayList()so I think there is no difference between them.

Lets see what others have to say for this.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Shweta S Sharma S" please check your private messages for an important administrative matter. You can see them by clicking the My Private Messages link above.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Preetha Arun:
Here we are not allowed to add anything other than String,why we are not calling this as type safe collection?

what is the difference between

List<String> li=new ArrayList();
and
List<String> li=new ArrayList<String>();?

please someone clear this..



List<String> li=new ArrayList(); is a type safe collection.

List<String> li=new ArrayList<String>(); is also a type safe collection.

There is no difference in the above two statements as far as enforcing type safety is concerned.

I had explained this in a similar post before with an example. Let me try once again.
List<String> li = new ArrayList(); is type safe but on slightly modifying the code above we can see the difference

imagine you were passed the ArrayList al by some other method and you assign it to li then you cannot guarantee that the ArrayList passed contains only Strings. e.g.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic