• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Array Doubt

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

The following code sample is taken from JQ+



The questions is if the above class will print '2' when compiled and run.

I chose true but its false

Below is the explanation given by JQ+ but its not clear to me

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. The embedded assignment of 2 to index occurs before the check for a null pointer.
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.

Could some one kindly clear up/explain it.
Waiting for a favorable reply.Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The questions is if the above class will print '2' when compiled and run.

I chose true but its false



The answer is wrong. If you compile and run the prog, you should get "index = 2" printed.... ie... the answer should be true.

Henry
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Henry,
Sorry for the mistake I made. My Bad . I chose false and the answer is true. Thanks in advance.
Kind regards.
Hasnain Javed Khan.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm a bit confused on the syntax of the this line:

getArray()[index=2]++;

I know getArray is a method. I hope someone would explain completely. Thanks!
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key to understanding this is to know that as assignment expression like "index=2" evaluates to the value being assigned, and that "++" increments the number/variable in front of it by 1. So that statement is equivalent to
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
My confusion is that the evaluation of dimension expression in the array access expression i.e [index=2] is evaluated first before the array reference is resolved or does it depend on the dimension expression inside the square brackets e.g array_ref[1] will resolve the reference variable directly and array_ref[index=2] will resolve the array dimension first and then resolve the array reference variable ??
Hope the question is clear and not confusing.
Waiting for a favorable reply. Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the question is a bit hard to understand due to the complete lack of punctuation.

The array index can't be resolved until the assignment is carried out, because the value of the array index is not known before that; does that answer the question?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Ulf is right.

Without the expression is evaluated, an array index can not be resolved!
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic