About Question enthuware.ocpjp.v8.2.1588 :
Moderator: admin
-
- Posts: 33
- Joined: Tue Jun 19, 2018 10:27 am
- Contact:
About Question enthuware.ocpjp.v8.2.1588 :
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."?
{
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."?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
"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.
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.
-
- Posts: 33
- Joined: Tue Jun 19, 2018 10:27 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
Got it! the second operand is optional error message used as the message for the AssertionError that is thrown.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jun 19, 2018 10:27 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
Thanks for the quick response
Appreciate it
Appreciate it
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
Hi Paul!
Why the assert code compile if we remove the 'throw' keyword?
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
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1588 :
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.
Who is online
Users browsing this forum: No registered users and 42 guests