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

Question #2 from Doug's book

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will this code compile? If it does, what happens when it runs?
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
Will this code compile? If it does, what happens when it runs?


compiles and runs fine?
my reason: the first statements creates 10 Object reference(as an array) points to 10 null string objects. And the second statement creates an object and make array[0] (a object reference) to this object.
is this the case?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,
For the Code following is the explanation :
a) At compile time
1) Object[] array = new String[10];
This is fine as we can assign String to Object.
2) array[0] = new Object();
Compiler checks if array elements are of type Object.
Hence the code compiles.

b) At run-time
as "array" was initialized containing String elements so it expects that each element should always be assigned to String objects only. So the assignment is invalid.
Hence gives java.lang.ArrayStoreException
For more details refer to:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11430
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
Will this code compile? If it does, what happens when it runs?


Compile but won't run.
Arrays are suppose to be a collection of elements of the same type. The array in the sample code was initialized to contain String. Although its reference array was cast as Object[], this is acceptable because the type Object[] is at the top just below Object.
When an element of type Object is stored at element 0, the program throws an exception because its type is different from the initialized type of the elements of the array.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler thinks it is an array of Objects so it compiles fine. Since the array is really an array of Strings, only String objects can be placed in it. Therefore it gets a runtime java.lang.ArrayStoreException.
For more cool stuff like this: http://www.javarules.com
[ August 08, 2003: Message edited by: Thomas Paul ]
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic