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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Please see this post. The writer has explained it very nicely about the meaning.
If you like our products and services, please help us by posting your review here.

The_Nick

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

The_Nick

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

Post by The_Nick »

Majestic explanation.
Thanks a lot.

The_Nick

thisOtterBeGood
Posts: 14
Joined: Wed Oct 09, 2013 3:13 am
Contact:

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

Post by thisOtterBeGood »

Really great answer!

Thank you!

SteveMoody
Posts: 1
Joined: Sun Sep 01, 2013 11:44 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

This is explained in my post above.
If you like our products and services, please help us by posting your review here.

Kolodets
Posts: 2
Joined: Mon May 22, 2023 5:46 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Please go through the posts above. Specially this one: https://enthuware.com/forum/viewtopic.php?p=4258#p4258
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests