Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

javac gives no type safety warning ?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

page 578 of the Study Guide says, that when calling a legacy method that add to a Collection, the compiler should give this warning:

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


I do not get this note/warning !? Neither in Eclipse or when running javac from command line (see below for details).

Content of file TestBadLegacy.java (mostly copied from book):


Content of file Inserter.java (mostly copied from book):


Commands used:
> javac -source 1.4 -target 1.4 other\Inserter.java
> javac -source 1.5 -target 1.5 -Xlint:unchecked stein\generics\mix1\TestBadLegacy.java

No warnings, with or without -Xlint:unchecked
JDK version is "1.5.0_15" in WinXP 32 bit.

java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) Client VM (build 1.5.0_15-b04, mixed mode, sharing)

Am I doing something wrong ? (it _was_ late at night...)

Regards,
David

PS: About my account, it is a temporary one, because my real one is stuck in the process of registration. I contacted the admins. Please no discussion of it in _this_ thread. Thanks.
[ May 15, 2008: Message edited by: Slowly Annoyed ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Slowly Annoyed ",
Please check your private messages regarding an important administrative matter.
-Ben
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the following message in eclipse for this code



Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic
type ArrayList<E> should be parameterized

Click on the project on the java perspective and press ctrl + enter. Make sure the java compiler settings point to java 5. Also ensure that you dont get compiler errors when you use generics. Perhaps you have warnings turned off somewhere ?
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Slowly Annoyed:

Commands used:
> javac -source 1.4 -target 1.4 other\Inserter.java
> javac -source 1.5 -target 1.5 -Xlint:unchecked stein\generics\mix1\TestBadLegacy.java



The unchecked warning occurs in the code in Inserter.java when compiled in Java 5 or greater.

By compiling it with the source/target 1.4, you're compiling it in a Java version that knows nothing of Generics, thus no unchecked warnings.

Go ahead and compile all your code with 1.5 (and you don't need the source/target switches if that's your compiler version) and the -Xlint flag.
 
David Bala�ic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, thanks.

So if I write new (generic) code and use a method (that takes a List and adds element to) it from my old non-generic (already compiled) code, the there will be no warning ?

Regards,
David
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Slowly Annoyed ",
Please check your private messages again.
-Ben
 
reply
    Bookmark Topic Watch Topic
  • New Topic