About Question enthuware.ocpjp.v8.2.1588 :

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

Moderator: admin

Post Reply
teodorj
Posts: 6
Joined: Tue Jun 19, 2018 10:27 am
Contact:

About Question enthuware.ocpjp.v8.2.1588 :

Post by teodorj »

void assertTest(Object obj)
{     
assert obj != null : throw new AssertionError();
}

Second operand must evaluate to an object or a primitive. (It can also be null)

What do you mean by "Second operand must evaluate to an object or a primitive."?

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

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by admin »

"Evaluate to" means the type of the result of evaluating the expression.
The assert statement may have two operands, the first operand is a condition, meaning, the result of the expression used as the first operand should be a boolean (true or false ). Similarly, the result of the expression used as the second operand must be either an object or a primitive (or a null).
Examples:

(In the following examples the expression used as the first operand is obj!=null. It evaluates to true or false.)

assert obj != null : 10; //second operand is an int primitive
assert obj != null : null; //second operand is null
assert obj != null : "string"; //second operand is a String object
assert obj != null : new Student(); //second operand is a Student object (assuming there is a Student class).
and so on.

If you have trouble understanding the above, I would suggest you to go through a good book first before attempting mock exams.
Paul.
If you like our products and services, please help us by posting your review here.

teodorj
Posts: 6
Joined: Tue Jun 19, 2018 10:27 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by teodorj »

Got it! the second operand is optional error message used as the message for the AssertionError that is thrown.

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

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

teodorj
Posts: 6
Joined: Tue Jun 19, 2018 10:27 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by teodorj »

Thanks for the quick response :thumbup:
Appreciate it :cheers:

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by bvrulez »

teodorj wrote:
Tue Jan 21, 2020 10:22 am
Got it! the second operand is optional error message used as the message for the AssertionError that is thrown.
Thanks!

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

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by Javier »

Hi Paul!
Why the assert code compile if we remove the 'throw' keyword?

Code: Select all

void assertTest(Object obj) {     
           assert obj != null : throw new AssertionError(); }// not compile

Code: Select all

void assertTest(Object obj) {    
           assert obj != null : new AssertionError(); }// compile

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

Re: About Question enthuware.ocpjp.v8.2.1588 :

Post by admin »

Rule of the language. The assert statement requires an expression. "throw new AssertionError();" is not an expression, "new AssertionError()" is. You might want to go through the difference between an expression and a statement from Section 6.1.2 of Hanumant Deshmukh's OCP Java 11 Part 1 Fundamentals: https://amzn.to/2PucBeT
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 2 guests