About Question enthuware.ocajp.i.v8.2.957 :

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

Moderator: admin

Post Reply
Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

About Question enthuware.ocajp.i.v8.2.957 :

Post by Javier »

Hi Paul!

Why is not compiling this method?

public static void changeIt(int a){
a+2;
}
and this neither?

public static void changeIt(int a){
int f=4;
a+f;
}
I think that something very basic I am missing but I don´t know exactly what...
Thank you very much Paul.

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

Re: About Question enthuware.ocajp.i.v8.2.957 :

Post by admin »

As per section 14.8 of JLS the only expression statements that are valid in Java language are:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression

a+2; or a+f; are, therefore, not valid although a = a+2; or a = a+f; are valid because they are valid assignments.

HTH,
Paul.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.957 :

Post by Javier »

and why is not possible to store the value of the method:

changeIt(someInt);// like int t= changeIt(someInt);

is it not possible because changeIt is void method???

Thank you so much Paul for the quick answer to my last question!!

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

Re: About Question enthuware.ocajp.i.v8.2.957 :

Post by admin »

Javier wrote:and why is not possible to store the value of the method:

changeIt(someInt);// like int t= changeIt(someInt);

is it not possible because changeIt is void method???
That is correct. Since changeIt doesn't return anything, you can't assign its value to int t.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests