Page 1 of 1

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

Posted: Mon Jun 23, 2014 9:52 am
by apanah
I'm not familiar with this expression:
a[val()]++

Can you break it down for me? I'm especially puzzled by the .

Thanks.

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

Posted: Mon Jun 23, 2014 8:46 pm
by admin
You know that [] is an indexing operator. You need an int inside of it. Now, i=1 is an assignment expression that means you are assigning 1 to i.

What is probably confusing is that every assignment expression has a value of its own, which is same as the left hand side of =. So the value of i=1 is 1.
Therefore, will be resolved to [1].

HTH,
Paul.

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

Posted: Fri Sep 26, 2014 8:42 am
by vijayanand
val() does not return int.

Does this is not have compiler error?

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

Posted: Fri Sep 26, 2014 9:34 am
by admin
Why do think there should be a compilation error? As far as the compiler is concerned, the method val() does return an int.

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

Posted: Fri Sep 09, 2016 5:44 am
by ErikSKan
Where does it? As far as I know the only way for a method to return something is with a return statement.

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

Posted: Fri Sep 09, 2016 10:34 am
by admin
ErikSKan wrote:As far as I know the only way for a method to return something is with a return statement.
That is true. However, at the time when the compiler is compiling the code fragment "a[val()]", it doesn't look at the code inside the method val() to check whether the method actually does return something. It only looks at the method signature to check whether this method is meant to return an int or not.

The compiler does go through the code inside the method val() but only when it compiles this method. At that time, it will try to make sure that the method either actually returns an int or throws an exception. Both are valid possibilities for a method that declares that it returns an int.

HTH,
Paul.