GeeCON Prague 2014*
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
Author

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


SCJP 5
Paul Lachance
Greenhorn

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;
sampleList.add(5);
}

GenTest3.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
sampleList.add(5);
^
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;
sampleList.add(5);

for(Object obj:sampleList)
System.out.println(obj);
}
}

Note: Test.java 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
 
GeeCON Prague 2014
 
subject: Generics and Warning message