• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Generics: warnings

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm reading in the SCJP for java 6 Study Guide (Sierra, Bates) that if one of my classes (TestLegacy) using say a List<Integer> calls a method from another class (Inserter) that does not use generic types, I should get a warning "TestLegacy.java uses unchecked or unsafe operations."

Now if I compile with the command-line:

javac TestLegacy.java
Note: ./Inserter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.



Note that it blames Inserter.java, and not TestLegacy.

If I remove TestLegacy.class and compile again, no single warning is issued. Now I'm using a 1.5 compiler, could it be that this is a difference between java 5 and 6?

Compiling with Xlint:unchecked gives this result:



here's the code:





[ December 04, 2008: Message edited by: jean-gobert de coster ]

[ December 04, 2008: Message edited by: jean-gobert de coster ]
[ December 04, 2008: Message edited by: jean-gobert de coster ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jean, welcome to javaranch.

The code is behaving as it should jean. Can you please elaborate a bit more what are you confused at??
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ankit,
my problem with this is that the Java 6 Study Guide says (page 603) that a warning should come from the TestLegacy class as well as the Inserter class.

The reason being:
If I am writing code using generics, and I am calling methods from an external package that I did not implement, that method could be adding Objects to my typed collections that is not of the right type (here String instead of Integer).

Since that external package is already compiled, I don't get any warning. Worse, I won't know that there is a problem until all hell breaks loose when I execute the code (and I won't even get an exception when this happens, so good luck debugging).

Edit: Oh, I just figured this out, in the example from the book, the Inserter and TestLegacy class are declared in the same file... But nevertheless, I do see a potential problem here. Why don't we get a warning when calling a method that uses non-generic Collections?

[ December 04, 2008: Message edited by: jean-gobert de coster ]

[ December 04, 2008: Message edited by: jean-gobert de coster ]
[ December 04, 2008: Message edited by: jean-gobert de coster ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that I am looking at your problem correctly as I answer this!

The compiler is telling you where the problem is. TestLegacy doesn't really have a problem. It defines a generic List reference and creates a generic ArrayList.

What the compiler is trying to tell you is that the method in the Inserter class takes the List as input but it is non-generic. It is an indication that the type safety of your collection is in doubt within this method because the compiler was not asked to protect it's type safety by specifying the parameter as generic.

I hope this helps.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic