I'm not familiar with this expression:
a[val()]++
Can you break it down for me? I'm especially puzzled by the .
Thanks.
About Question enthuware.ocajp.i.v7.2.959 :
Moderator: admin
-
- Posts: 5
- Joined: Sat Jun 21, 2014 3:49 pm
- Contact:
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.959 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Fri Sep 26, 2014 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.959 :
val() does not return int.
Does this is not have compiler error?
Does this is not have compiler error?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.959 :
Why do think there should be a compilation error? As far as the compiler is concerned, the method val() does return an int.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Sep 02, 2016 3:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.959 :
Where does it? As far as I know the only way for a method to return something is with a return statement.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.959 :
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.ErikSKan wrote:As far as I know the only way for a method to return something is with a return statement.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 239 guests