This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics warnings... I don't understand?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics warnings... I don Watch "Generics warnings... I don New topic
Author

Generics warnings... I don't understand?!

al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
Hy guys
I'm studying for scjp. I'm doing some test with generics in Eclipse and I've some questions
I'm learning using the scjp mcgraw hill's book .. it shows 2 examples
1st:
----
import java.util.*;
public class TestLegacy {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
// type safe collection
myList.add(4);
myList.add(6);
Adder adder = new Adder();
int total = adder.addAll(myList);
// pass it to an untyped argument
System.out.println(total);
}

}

import java.util.*;
class Adder {
int addAll(List list) {
// method with a non-generic List argument,
// but assumes (with no guarantee) that it will be Integers
Iterator it = list.iterator();
int total = 0;
while (it.hasNext()) {
int i = ((Integer)it.next()).intValue();
total += i;
}
return total;
}

--------
So the book says that no warnings are generated.. this because the Adder's method 'addAll' don't modify the type safe List passed in.
So I try to compile the Adder class with java1.4 and TestLegacy with java6 and no warnings comes out.. so OK!

the 2nd example:
--------
import java.util.*;
public class TestBadLegacy {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
myList.add(4);
myList.add(6);
Inserter in = new Inserter();
in.insert(myList); // pass List<Integer> to legacy code

}
}

class Inserter {
// method with a non-generic List argument
void insert(List list) {
list.add(new Integer(42)); // adds to the incoming list
}
}

--------

this time I've understand that "in.insert(myList);" should give a warning!?
This because Inserter modify the list in insert() with an add..(right??).
So I compile Inserter with J1.4 and TestBadLegacy with J6 but the compiler doesn't give me a warning like 'unchecked or unsafe operations'
I need to understand exactly when are generate warnings when I use generics with old legacy code.
Thanks for your help.


SCJP5 - SCWCD5 - SCBCD5
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Hi
In regard to SCJP 5.0 you should do all your testing using JDK 5.0 and a simple text editor Not IDE like Eclipse. Things should work properly then.
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
thanks for your response.
so ok I try to use cmd and notepad..
I compile Inserter with 'javac -source 1.4 -target 1.4 Inserter.java'
and TestBadLegacy with 'javac TestBadLegacy.java'
when I call the last one I expect a warning.. the compiler gives no warning
I don't understand when exactly java5 code that use generics generate warnings using legacy code?
[ October 19, 2007: Message edited by: al nik ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy Al,

welcome to the Ranch!



No, the line in TestBadLegacy does not give a warning, also with the original compilers from Sun.

class Inserter compiles without a warning when compiling with Java version 1.4, simply because there are no generics before Java 5.
It compiles with a warning in the add line with Java 5 just as expected.

But the line with the insert call in class BadLegacy does NOT give a warning. You get warnings when you assign like
List<Integer> myList = new ArrayList();

but not if a legacy method operates on your generic List object.


The same occurs with Eclipse.

Yours,
Bu.


all events occur in real time
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
Hi thanks a lot for your response, this's a great forum!
So the problem was that I put the two classes (TestBadLegacy, Inserter) in two different file .java .
I merge the 2 classes in one unique file TestBadLegacy.java and now the compiler prints the warning. If I remove the .add() from Inserter the warning disappear. so I get it.
So the rule to remember here is that generics warnings comes out only when a legacy method add something to a type safe list we passed in.. or in general when the add() is called from an 'untyped var' like 'List list'
I think that the problem I had when I used 2 different files was because the TestBadLegacy takes the .class of Inserter from the 2nd call of 'javac TestBadLegacy.java' and apart from the 1st call I don't get warnings.
So situations of warning like this and this one
List<Integer> myList = new ArrayList();
are the only I can encounter on the exam?
[ October 20, 2007: Message edited by: al nik ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Al asked
So situations of warning like this and this one
List<Integer> myList = new ArrayList();
are the only I can encounter on the exam?


No, there are three major situations where you get the warning.

First you mentioned, when you assign a non-generic object to a generic variable.
Summary of the first:
Question:
List a = new ArrayList();
List b = new ArrayList<Integer>();
List<Integer> c = new ArrayList();

Which of them raise warnings?



Answer:
only line 3.


The second case is when you have a non-generic object of a generic class and you change any of its generic variables. Most often by adding something to a non-generic collection but also like in this example:



The third is casting, e.g.
generic = (Bag<Thread>) legacy;
or

static <T> T foo (T t, Object o) {
return (T) o; <--- warning
}


There are also different warnings that have nothing to do with Generics.
But I am quite sure that they are not on the exam at all. The only example that comes to me head is:

System.out.printf("%b",null);


And since you mentioned the Eclipse IDE, in Eclipse you will have additional warnings about unused local variables, unused imports and static access via an object that are Eclipse specific and of course not part of the exam.



Yours,
Bu.

edits: graemlin removed & ;) deleted
[ October 20, 2007: Message edited by: Burkhard Hassel ]
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
You clarified me the concept. thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics warnings... I don't understand?!