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

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

Moderator: admin

Post Reply
apanah
Posts: 5
Joined: Sat Jun 21, 2014 3:49 pm
Contact:

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

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

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

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

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

vijayanand
Posts: 10
Joined: Fri Sep 26, 2014 8:40 am
Contact:

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

Post by vijayanand »

val() does not return int.

Does this is not have compiler error?

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

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

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

ErikSKan
Posts: 2
Joined: Fri Sep 02, 2016 3:19 am
Contact:

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

Post by ErikSKan »

Where does it? As far as I know the only way for a method to return something is with a return statement.

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

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

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