This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exam lab2(question 34)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

above code compiles fine.
line1 i think this is returning String [](String array),then how it can be assigned to Object type safe list?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but I guess it(string array) is considered as an Object(since arrays are Objects) and added to a list which accepts Object arrays.

Someone please correct me if I am wrong.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:I am not sure but I guess it(string array) is considered as an Object(since arrays are Objects) and added to a list which accepts Object arrays.
hi neha thanks for reply
i think you are right if above line compiles fine.but how,it is voilating the rule of generics.
btw this is the actual question

 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjun!

With arrays you can do an assignment such as the subtype array can be treated as the supertype array. In other words, following is correct:



as the array of Strings can be treated as array of Objects.
Moreover, when talking about generics, following is correct:



as you can add a subtype of an Object to the Object list.

When you combine the first feature with the second one into the following, the add operation is valid:


as the String[] is an array of subtypes of Object.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I guess any array can be assigned to an array of its superclass.For example if class B extends class A then,
A[] a = new b[5];
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh its already answered.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pedro and neha,for your precious time.
your answers are worth while and understandable,i have made a note of it already
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem mate!

Glad we could help :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic