Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Sybex Chapter 4 - Question 28

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anybody help me to understand this question and answer?
I can't link line 7 en 8 to OCA Fundamentals and key learning point for exam.
What exactly happens in these two lines?
Though I understand you can't assign a String to an int,
and I'm trying to unravel how to avoid ArrayStoreException in this example.
How can test if obj[3] is really an int[] behind the scenes and not an int.


28. What is the result of running the following program?



A. X
B. The code does not compile.
C. The code compiles but throws a NullPointerException at runtime.
D. The code compiles but throws a different exception at runtime.


28. D.
Line 6 assigns an int to a cell in a 2D array. This is fine. Line 7 casts to a general Object[]. This is dangerous, but legal. Why is it dangerous, you ask? That brings us to line 8. The compiler can’t protect us from assigning a String to the int[] because the reference is more generic. Therefore, line 8 throws an ArrayStoreException because the type is incorrect, and Option D is correct. You couldn’t have assigned an int on line 8 either because obj[3] is really an int[] behind the scenes and not an int.
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that arrays are covariant, so it S is a subtype of T, then S[] is a subtype of T[].
An int[] is an Object, but it isn't an Object[]. Its elements are primitives, not objects.
An int[][] is an array containing int[]s, and those contained int[]s are Objects. So an int[][] is a subtype of an Object[]. So you can assign an int[][] to a reference of declared type Object[]. So all the code will compile. When you try to assign a String to an element of an array in an Object[][], the compiler will have to accept it because a String is a subtype of Object. At runtime, however, the class type of your String is tested; it should be an int[] to match the runtime type of your array elements. But it doesn't.

That code will compile all right and at runtime crash with an exception: probably one of this sort.
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Stam wrote:. . . I understand you can't assign a String to an int,

You aren't. You are assigning a String to an Object, but you later find out that Object's runtime type is int[].

. . .
How can test if obj[3] is really an int[] behind the scenes and not an int. . . . .

With getClass() or instanceof. Of course, once you start using that sort of coding outside an equals() method, you are straying beyond the realms of good object‑oriented programming. Remember that cert exams and cert exam revision books can show you bad code and you answer the question by working out what is wrong with the code. Remember that getClass() and instanceof won't compile with primitives.

Please don't copy line numbers; the code tags insert line numbers automatically.
 
Jeff Stam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So you can assign an int[][] to a reference of declared type Object[]. So all the code will compile.



I would like to understand how I can access and print the Object[] elements and beside that how to check what type game is ?

game instanceof Object is always true right?
and
obj[0].getClass().getComponentType() returns int
and
game.getClass().getComponentType() returns class [I

Can you help me to understand this objective so I can learn?
I really will appreciate it, thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!