Page 1 of 1

About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 21, 2013 11:08 am
by muttley
Isn't it better <boolean_expression> : <any_expression_but_Notvoid>? Because the expression can't return void. With <any_expression_but_void> seems that the return may be void.

Tks

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 21, 2013 11:40 am
by admin
I guess it could be interpreted that way. You could have a method that returns void and that would be invalid as well. So, in a way, you can have an expression that results in void.

-Paul.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 21, 2013 12:15 pm
by muttley
Sorry, but now I am a little confuse.
For this statement:

Code: Select all

public void assertTest(List v) {
    assert v.size() == 10 : v.clear();
}
The explain say:
"Invalid because List.clear() returns void and  boolean : void does not satisfy <boolean_expression> : <any_expression_but_void>, which is required for an assert statement."

I understood that this question is wrong because return void and you just answer that can do it.
Could you explain a feel better.
I know, I'm making mess alone, but I don't know where.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 21, 2013 12:31 pm
by admin
No, I said, "You could have a method that returns void and that would be invalid as well."

My statement, "So, in a way, you can have an expression that results in void." was in response to your statement, "Because the expression can't return void. "

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Wed Nov 09, 2016 10:47 pm
by Sai Divya sree
can second expression of assert statement be an object reference?I mean just the reference not the actual object.
Like in this case:
/code
public void assertTest(List v){
assert v.size()==10: v ;
}
/code
I am preparing from study guide by K&B.It says,
illegal assert statement :
assert(x==1):vallidAssert va;
Because the statement doesn't return a value.
So I am little confused if references are valid as second exp in assert statement?

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 10, 2016 1:04 am
by admin
What happened when you tried it out?
BTW, in Java you always refer to an object using a reference. So I am not sure what you mean by just the reference not the actual object.
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 10, 2016 1:27 pm
by Sai Divya sree
with just a reference :(Illegal assert statement)
assert(x==1):vallidAssert va; // do not return a value.
with object:(Legal assert statement )
assert(x==1):new validAssert();
As per the book... The second expression,used only with simple version of an assert statement ,can be anything that results in a value.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Thu Nov 10, 2016 9:20 pm
by admin
assert(x==1):vallidAssert va;
This is illegal syntax. Not sure why are you expecting it work. You should go through this to understand the syntax clearly: http://docs.oracle.com/javase/7/docs/te ... ssert.html

Or check with the book authors what do they mean about it.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Wed Feb 19, 2020 8:09 am
by bvrulez
What is the return value of "v = new ArrayList<>();" ?

Re: About Question enthuware.ocpjp.v7.2.1406 :

Posted: Wed Feb 19, 2020 12:03 pm
by admin
v = new ArrayList<>(); is an expression. Every expression itself has a value (which, in this case, is same as the value being assigned to v).