File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes tiger - doubt in this code ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "tiger - doubt in this code ..." Watch "tiger - doubt in this code ..." New topic
Author

tiger - doubt in this code ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


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

Joined: Aug 31, 2004
Posts: 11343

That's right.

(Have you been able to install Tiger yet?)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
(Have you been able to install Tiger yet?)


No marc ,
Thanks .
[ March 01, 2005: Message edited by: rathi ji ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 11, 2004
Posts: 3830
Thank you very much marc .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: tiger - doubt in this code ...
 
Similar Threads
Simple J2SE 5.0 Tiger Notes
Generics and More Generics...
Generics query
Generics -really very hard question
iterator()