This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question #2 from Doug's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question #2 from Doug Watch "Question #2 from Doug New topic
Author

Question #2 from Doug's book

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Will this code compile? If it does, what happens when it runs?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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?


Meng Yi
Cody Beth
Greenhorn

Joined: May 20, 2002
Posts: 11
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
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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 ]
 
jQuery in Action, 2nd edition
 
subject: Question #2 from Doug's book
 
Similar Threads
class cast exception
Array's
array objects
Casting Object arrays to String arrays
Simple questions on arrays