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

ArrayList

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


has List l=new ArrayList(); been totally removed...nothing seems to be working with it?
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without the generic parameters from the compiler's point of view the list will look as follows:



List.add() will accept an Object as parameter.

Floating point literals are doubles by default, and the compiler can box it into a Double, which is a descendant of Object that the add method accepts.

With the Integer generic parameter, however, the parameter of List.add() is an Integer, and there is no rule in the language for converting a double or Double to Integer when matching parameter types, so the compiler won't accept it.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whether it is a double or an int there is a compile error...
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankur

I ran below code

It compiled fine and gave me output as 2.

If change code to add generics part as shown in below code


here i get compile time error which is expected as we are trying to add double value in a list that takes only Integer

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(Integer) in the type List<Integer> is not applicable for the arguments (double)



Hope it clears

Check which Java version are you using to compile your code as Autoboxing feature was not present before Java 5 version. You might getting error because you may be running your code on lesser version of Java (Java 1.4,1.3 etc)

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


oh am sorry...actually when i compile the program, it compiles but gives a warning that program uses unchecked operations.....i thought this was a compile error but it is a warning because when i ran it i got the output 2

Thanks everyone for your efforts..

 
reply
    Bookmark Topic Watch Topic
  • New Topic