Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Fri Jun 01, 2012 5:32 pm
by ETS User
Hello
Is this question referring to array access or array reference expression
as per the explanation. At first it says "index expression must be completely evaluated before any part of the indexing operation occurs, and that includes the check as to whether the value of the LH operand is null." Lower down it says in "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."

What does it mean by the expression to the left of the brackets?? Can somebody clarify?
Thanks alot!

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Sat Jun 02, 2012 7:32 am
by admin
Please see this post. The writer has explained it very nicely about the meaning.

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu Mar 07, 2013 3:47 am
by The_Nick
Hi,
regarding this question, I would like as already been notified above the contradiction between 2 statements present in the explanation.
A NullPointerException never occurs because the index expression must be completely evaluated before any part of the indexing operation occurs, and that includes the check as to whether the value of the left-hand operand is null.
...
...
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.
So basically as stated in the previous post above, it's not clear whether the index part is first evaluated as stated in the first part of the explanation from you provided or the expression to the left of the brackets is first evaluated as stated in the second part of the Explanation. (The relevant part referring to what I am talking about are underlined).

Does the first bit refer to a process before array access and the second bit to the array access? if so what is the process happening before the array access? I would like to know what's the difference between the 2 bits underlined in the quoted statement above.

Thanks very much in advance.
The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu Mar 07, 2013 7:17 am
by admin
There is no contradiction. For example consider this line of code:


Object o = getArray()[getIndex()];
(Assume that the method getArray returns an object array and getIndex returns an int)

1. The expression to the left of the brackets is evaluated first. So first getArray() will be called. This gives the reference to the array. It could be null as well. (But if the method throws exception, the rest of the statement will not be evaluated i.e. getIndex() will not be called.)

2. Now, the expression in the brackets is evaluated, so getIndex() will be called, which will give you the index. If getIndex throws an exception then step 3 will not happen.

3. Finally, the index will be applied on the array. At this time, if the array reference is null, you will get a NullPointerException.


I would suggest you to write a small program and try it out.
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu Mar 07, 2013 7:39 am
by The_Nick
Majestic explanation.
Thanks a lot.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Fri Nov 01, 2013 1:39 am
by thisOtterBeGood
Really great answer!

Thank you!

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu Feb 27, 2014 1:40 am
by SteveMoody
I originally thought this would not compile, since the method m1() returns an int, but no return statement is included in the method. So, apparently no return is required if an exception is guaranteed to be thrown. Is this correct? Also, are there any other situations that do not require a return statement in a method that states it will return a value or object?

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu Feb 27, 2014 10:26 am
by admin
SteveMoody wrote:I originally thought this would not compile, since the method m1() returns an int, but no return statement is included in the method. So, apparently no return is required if an exception is guaranteed to be thrown. Is this correct?
yes.
Also, are there any other situations that do not require a return statement in a method that states it will return a value or object?
Yes. Example:

Code: Select all

public int x(){
  while(true){ }
}
You may try and think of more such situations.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Sat May 17, 2014 9:27 am
by UmairAhmed
As stated in the explanation

/* 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.

Here, m1() is called first, which throws Exception and so 'a' is never accessed and NullPointerException is never thrown. */

What does that mean by the above statement, I got this explanations as
All the expression before the brackets will be solved, and if they abrupt, exception will be thrown right from there.
But then it states that method m1() will be called first, how .. i mean isn't the variable 'a' points nothing (null), and being on the the left side of bracket, how some method can be called upon null and not throwing the null pointer exception.

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Sat May 17, 2014 12:24 pm
by admin
This is explained in my post above.

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu May 25, 2023 8:23 pm
by Kolodets
"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"
There seems to be contradiction
The part in the brakets will be evaluated first,so Exception will be thrown and the expression to the left will not be evaluated

Re: About Question enthuware.ocajp.i.v7.2.1311 :

Posted: Thu May 25, 2023 9:27 pm
by admin
Please go through the posts above. Specially this one: https://enthuware.com/forum/viewtopic.php?p=4258#p4258