Help coderanch get a
new server
by contributing to the fundraiser
  • 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 about generic collection

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

There is something I don't understand I hope someone can help me.

What is the difference between :

List<String> x1= new ArrayList<String>();

and

List<String> x1= new ArrayList();


They both compile and the compilator is giving me a warning for the second one but I don't understand the problem. What is the risk of using the seconf one?

Thank you
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is very interesting in my opinion because you get a warning about type safety but in reality you get a compile time error if you try to add a non-string object into the second List... so it almost seems like Java
is making the conversion for you and in essence the two are identical (except for the warning of course).

In C# there isn't a generic ArrayList implementation so you get a compiler error. The CLR (in .NET) also won't allow you to do the following:
List<String> list = new Stack(); (where both generic and non generic implementations for stack exists).

Hope that helps.
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the response. It is what I was thinking, I don't see any
difference between the 2 declarations. They behave identically except from the warning at compilation. I have to keep that in mind for the exam.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
List x1= new ArrayList<String>();

Why do you think it allows any add operation on x1 even though the actual ArrayList object is of generic type- String.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


What is the difference between :

List<String> x1= new ArrayList<String>();

and

List<String> x1= new ArrayList();



The first one here is a proper type safe ArrayList. There can't be anyything thats not a String in that ArrayList. Here the second one isn't necessarily a type safe list...

Its something like this....





Here the compiler accepts such a line but warns you that your list x2 which is supposed to be typesafe "may not be typesafe"... (true in this case..)


say I comment the line 1, it still would warn you...

GenericType<-NonGenericType :compiler warning

NonGenericType<--GenericType :Again a compiler warning

Generic<-Generic :No probs

NonGeneric<-NonGeneric : No probs

Regards,
Vishwa
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nadeem,

As per my understanding , we have assigned here generic type to raw type.This is not useful in terms of type safety....

It allows to add any type of object BUT with warnings but we have to keep fingers crossed while iterating/retrieving (it may collapse at runtime due to this ), as we may be assuming that always String will come out but that can be anything Cat or Dog

I came across such question in mocks of whizlabs

Correct me please if i am wrong anywhere..

Thanku
Vishal
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Vishal,
You are correct but still i dont feel satisfied as what could be the reason that java allows this to happen!
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I have found out is.....
List<String> x1= new ArrayList<String>(); // Gen Gen OK
List<String> x2= new ArrayList(); // Gen NonGen Warning
List x3= new ArrayList<String>(); // NonGen Gen OK
List x4= new ArrayList(); // NonGen NonGen OK

Java allowed this... could be due to the same pattern occurs when you pass a Type Safe variable to a Non Type Safe method.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Didnt get you Nuwan. Please be a bit more elaborate.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic