About Question enthuware.ocajp.i.v7.2.1311 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1311 :
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!
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!
-
- Site Admin
- Posts: 10216
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
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.
Re: About Question enthuware.ocajp.i.v7.2.1311 :
Hi,
regarding this question, I would like as already been notified above the contradiction between 2 statements present in the explanation.
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
regarding this question, I would like as already been notified above the contradiction between 2 statements present in the explanation.
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).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.
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
-
- Site Admin
- Posts: 10216
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
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.
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.
Re: About Question enthuware.ocajp.i.v7.2.1311 :
Majestic explanation.
Thanks a lot.
The_Nick
Thanks a lot.
The_Nick
-
- Posts: 14
- Joined: Wed Oct 09, 2013 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
Really great answer!
Thank you!
Thank you!
-
- Posts: 1
- Joined: Sun Sep 01, 2013 11:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
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?
-
- Site Admin
- Posts: 10216
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
yes.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. Example: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?
Code: Select all
public int x(){
while(true){ }
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Apr 28, 2014 9:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
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.
/* 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.
-
- Site Admin
- Posts: 10216
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
This is explained in my post above.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Mon May 22, 2023 5:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
"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
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
-
- Site Admin
- Posts: 10216
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1311 :
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.
Who is online
Users browsing this forum: nkaragulov and 3 guests