• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Varargs parameter & generics

 
Ranch Hand
Posts: 80
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know the relationship of regular Object array used for refiable object like String vs Object array used for non-refiable such as generics.

When objects reference variable points to the String array, it can ONLY store String value into the array. This is for regular array.


Now, List<String> ... arrayOfListString is non-refiable. It is varargs argument in the method MyArray.
Java will turn List<String> ... arrayOfListString into Object [] array.
So I try to play around with line 3 and line 4. See below.
My question is line 2 where Object[] objects is pointing to arryOfListString which is an array of List<String> of type String.
On line 4, why can objects[0] point to different type of ArrayList of type Integer? Why not ArrayList of type String only?
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link to a blog post I just tracked down which explains why generics and arrays don't work together in Java: Why is Generic Array Creation not Allowed in Java?. (In the example you posted, you have an array of List objects and not an array of List<anything> objects.
    Bookmark Topic Watch Topic
  • New Topic