• 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

First time i see this kind of code

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic