• 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

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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
al nik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You clarified me the concept. thanks a lot
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic