About Question enthuware.ocpjp.v8.2.1399 :

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

Moderator: admin

Post Reply
Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

About Question enthuware.ocpjp.v8.2.1399 :

Post 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

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

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

Post by admin »

Since && is a short circuiting operator, if number is null, number.length() part of the expression will not be executed.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

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

Post by Mark7777 »

Thanks for replying. But is that syntactically correct? I'm guessing you think it is.

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

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

Post by admin »

Yes, it is syntactically correct. No compilation error.
You can actually try it out with a simple test program.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests