File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics and Warning message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics and Warning message" Watch "Generics and Warning message" New topic

Generics and Warning message

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Why line 1 give us no warning?? imo, line 1 is more dangerous than line 2.

and in the Real exam, should we expect this kind of "warning" as one of the choices? is it depend on the JVM implementation (Sun or IBM)?

thanks all

ArrayList sampleList = new ArrayList<Integer>(); // #1 - No warning

ArrayList<Integer> sampleList2 = new ArrayList(); // #2 - unchecked assignment

Paul Lachance

Joined: Nov 27, 2006
Posts: 10
I'm not sure what purpose #1 could serve but it seems you do get a warning if you try to add anything to sampleList:

public static void main(String [] args) {
ArrayList sampleList = new ArrayList<Integer>(); // #1 - No warning
int x = 5;
} warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
1 warning
Manju Devarla
Ranch Hand

Joined: Dec 14, 2006
Posts: 85
For both declarations we are going to get warning message

import java.util.*;

class Test {

public static void main(String [] args) {
ArrayList sampleList = new ArrayList<Integer>(); //#1 Warning

//ArrayList<Integer> sampleList = new ArrayList(); //#2 Warning
int x = 5;

for(Object obj:sampleList)

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

ie at #1 we are assigning generic(type safe) to non generic and
at # 2 doing otherway so both are unsafe or unchecked opearations
I agree. Here's the link:
subject: Generics and Warning message
It's not a secret anymore!