This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic question" Watch "Generic question" New topic
Author

Generic question

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hello,

I have read Kathy's SCJP 5 and according to its explanation, this code seems confusing to me.

In the following code:
import java.util.*;
class ListTest {
public static void main ( String [] args ) {
List < String > least = new ArrayList < String > ();
List list = new ArrayList();
meth(list);
seth(least);
}
public static void meth(List < String > list) {
System.out.println("List");
}
public static void seth(List list) {
System.out.println("Mist");
}
}


I get the following output:
javac -Xlint:unchecked ListTest.java
ListTest.java:6: warning: [unchecked] unchecked conversion
found : java.util.List
required: java.util.List<java.lang.String>
meth(list);
^
1 warning


Now my problem is, the warning I get is because I am putting a non-generic List in to a method that expects a <String> list. Although I didn't come across this in the book, intuitively it makes sense.
But the problem is, why am I NOT getting a warning when I should be for the 'seth' method? because I am putting a generic <String> List into a non-generic method.
And according to the book, the compiler should warn me because that non-generic method can add anything to my generic List. But in this case, the compiler isnt warn me when I do 'seth(least)'?

Thank you.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

try this seth method



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Ankit. Got it now. I get the warning only when if something is being added to in the method.
Thank you.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Ankit is right. Basically, the compiler warns you when type safety might be compromised at runtime. In the first case, type safety is compromised on assignment, even if you don't add anything to the list, because you don't know what the raw List contains, and you are assigning it to a List<String>. Even if you don't add anything you might encounter troubles at runtime. For example, you could perhaps iterate through the elements, applying toUpperCase() (a String method), but if the List had something other than Strings, this would result in an exception.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic question
 
Similar Threads
collections doubt
Method with Generic Argument
Doubt i a generics question
Generic Method declaration
Mixing genric and non-genric code in assignment