File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics..

 
Preethi Dev
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if this has been discussed already...please someone give me the link.
 
Shwetha Sharma
Ranch Hand
Posts: 34
Eclipse IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic