• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Varargs parameter & generics

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

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?
Posts: 25594
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.
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic