Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Compile fails (generics/collections)

 
Greenhorn
Posts: 16
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was expecting line 1 and line 2 to compile ...



However, only Line 1 is Compiled .
can you please explain why the two last lines (2&3) doesn't compile ?
 
Greenhorn
Posts: 10
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since element type of myList is unknown, object cannot be added to it. Add method will take argument of type same as element type of collection. ? is unknown type and parameter passed to add needs to be subtype of ? i.e. unknown type and hence nothing can be passed to add method except null in this case.
 
Rachid Aourich
Greenhorn
Posts: 16
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ,
in that case i dont see the utility of that Instantiation [ArrayList<?> myList=new ArrayList<String>(); ] :



can you please explain why the first one compiles and the second one no ? Im very confused about that .
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Reference declared as List<? super Integer> can point List containing objects of unknown type, which is a supertype of Integer. It could be List<Object>, List<Comparable>, List<Serializable>, List<Number> or List<Integer> (any of them).
When you call List.add() in this case you can safely do it only using Integer instance, which IS-A contemporary Object, Number, Serializable, Comparable and Integer. Regardless of which supertype of Integer is used in particular listOne initialization (you have chosen ArrayList<Integer>, but it could be ie. ArrayList<Serializable>), add() method can always be called with Integer instance. In contrary you could not call listOne.add(new Object()) when declared listOne reference as List<? extends Integer>, because this declaration could mean List<Comparable>, and Object cannot be cast to Comparable when using add() method (like Integer could be).



Here you declared listTwo as List of unknown types extending Integer. Let's suppose Integer is not final, and have subclass called SubInteger. List<? extends Integer> could be List<Integer> or List<SubInteger>. If it is List<SubInteger> you could not call add(Integer), because Integer is not SubInteger (SubInteger IS-A Integer, but not vice versa). It may look strange because we can see that listTwo is initialized as ArrayList<Integer> and not as ArrayList<SubInteger> (so we know the type is Integer not SubInteger), but please notice, that add() method is called on reference declared as List <? extends Integer> (uknown type).

Rachid Aourich wrote:
in that case i dont see the utility of that Instantiation [ArrayList<?> myList=new ArrayList<String>(); ] :



take a look here: https://coderanch.com/t/559717/java-programmer-SCJP/certification/List

regards
t.
 
Rachid Aourich
Greenhorn
Posts: 16
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Tomasz ,
your response was very helpful
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome ;]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic