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

Generics and Warning message

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic