• 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

tiger - doubt in this code ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this in one document :


1] This method should be called with a parameter of type List<String>, but it can be called with a parameter of type List
2]The disadvantage is that the compiler won�t catch errors . instead, errors will cause a ClassCastException
3] This is necessary for backward compatibility
Similarly, the Iterator need not be an Iterator<String>

But I am cofuse with first point . why it allow to call this method with any list ( I mean , a list which is not paramaterized by String ( can have other than String ) )

please help ...
Thanks a lot .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
... But I am cofuse with first point . why it allow to call this method with any list ( I mean, a list which is not paramaterized by String ( can have other than String ) )...


The parameterized type is List<String>. To allow for backward compatability, a non-typed List will also be accepted (compiling with a warning, but no error). However, a List with a type parameter other than String will not compile.
[ March 01, 2005: Message edited by: marc weber ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Means this is acceptable :

List myList = new ArrayList();
printListOfStrings(myList); // just show warnings

but this will give compile time error :

List<Integer> myList = new ArrayList<Integer>();
printListOfStrings(myList); // compile time error

Is this right ?
Thanks a lot .
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right.

(Have you been able to install Tiger yet?)
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more doubt is , How can we solve this problem

It may show ClassCastException because of user has entered a list that is not paramaterized ( list of 1.4 ) ...

How can we avoid that ...

Thanks a lot .
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(Have you been able to install Tiger yet?)



No marc ,
Thanks .
[ March 01, 2005: Message edited by: rathi ji ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
... How can we solve this problem
It may show ClassCastException because of user has entered a list that is not paramaterized ( list of 1.4 ) ...

How can we avoid that ...


You can write your own code to verify the runtime type of each List element before attempting to cast:

Object ob = iter.next();
if(ob instanceof String) { //cast to String }
else //avoid cast exception
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what you would actually see compiling the above examples in Tiger...

Compiler error using List<Integer>:

...printListOfStrings(java.util.List<java.lang.String> ; ) in ClassName cannot be applied to (java.util.ArrayList<java.lang.Integer> ; ) ...

Compiler warning using non-typed List:

Note: FileName.java uses unchecked or unsafe operations...
[ March 01, 2005: Message edited by: marc weber ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much marc .
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic