• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and Warning message

 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic