• Post Reply Bookmark Topic Watch Topic
  • New Topic

First time i see this kind of code  RSS feed

 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, recently i found this code. And i dont understand very well what is doing and why is working like that.



Íf i put

instead of line 12 i have compilation error. Honestly, i dont know whay is the code doing right here.

Someone can explain it to me? I suppose is a very stupid question and quite easy to understand, but ive never seen this kind of nomenclature before.

Thank you so much!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javier,
Obviously this is not a good way to write code as it is confusion. But here's what's going on:

1) Create index variable with value of 1.
2) Enter try block
3) Set index variable to 2
4) Try to access the second element of the null array and through a null pointer.
5) Catch and ignore the null pointer
6) Print out the value 2

Your rewrite doesn't work because [index=2] can't be used without an array immediately before.
 
Thanigai arasu Ruthirakotti
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Javier,

1. getArray() method will return the null value and getArray()[index=2]++ throws the Null pointer exception. You handled the exception, so the program won't terminate and it prints the index value as 2 .

2. getArray()[index=2]++;

Here you are accessing the interger array index value and increment the value by 1. If you terminate the getArray();, then how will you access the array index. Then the compiler will throw the error.

I would recommend you to read the basics of Array.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was that code posted on 1st April? It contains some really bad design like returning null and embedding an assignment in the square brackets [].
 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, got it!

The code is part of a OCA´s exam, but how is written is really confusing.

Thanks to all!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's almost identical to an Enthuware question from ocajp7. Here's the explanation:

If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.

This means, first index = 2 will be executed, which assigns 2 to index. After that null[2] is executed, which throws a NullPointerException. But this exception is caught by the catch block, which prints nothing. So it seems like NullPointerException is not thrown but it actually is.

In other words, the embedded assignment of 2 to index occurs before the check for array reference produced by getArray().

In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People often write certification exam code badly, using incorrect indentation and other poor features, so as to confuse the candidate into thinking it is other than what it actually is.
 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers!
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:People often write certification exam code badly, using incorrect indentation and other poor features, so as to confuse the candidate into thinking it is other than what it actually is.


And people wonder why the certification questions annoy me...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!