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 is really an int behind the scenes and not an int.
28. What is the result of running the following program?
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.
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 is really an int behind the scenes and not an int.
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 ints, and those contained ints 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.
posted 2 weeks ago
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 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.
posted 2 weeks ago
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?
obj.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.