Page 1 of 1

About Question enthuware.ocpjp.v8.2.1399 :

Posted: Thu Dec 31, 2020 9:54 pm
by Mark7777
If 'number' is null, then won't number.length() == 10 throw a NullPointerException? Which would be another reason why this code will not work in all situations if assertions are enabled? And would that make the code syntactically incorrect? Or does this have nothing to do with syntactical correctness?

public void processPhoneNumber(String number) {
assert number != null && number.length() == 10 : "Invalid phone number"; ... }

Thanks

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

Posted: Thu Dec 31, 2020 11:32 pm
by admin
Since && is a short circuiting operator, if number is null, number.length() part of the expression will not be executed.

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

Posted: Fri Jan 01, 2021 2:38 pm
by Mark7777
Thanks for replying. But is that syntactically correct? I'm guessing you think it is.

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

Posted: Sat Jan 02, 2021 12:17 am
by admin
Yes, it is syntactically correct. No compilation error.
You can actually try it out with a simple test program.