About Question enthuware.ocpjp.v8.2.1907 :

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

Moderator: admin

Post Reply
RoyEL1
Posts: 10
Joined: Tue Aug 09, 2011 3:23 pm

About Question enthuware.ocpjp.v8.2.1907 :

Post by RoyEL1 »

I find it a bit curious w.r.t.

Code: Select all

public void outputText(PrintWriter pw, String text) {
         pw.printf(text).print("success"); 
}
that the execution works, but the Java Platform Standard Ed. 8 (JDK SE8) Documentation states that for a PrintWriter, the printf(String format, Objects ... arg) and the printf(Locale l, String format, Objects ... arg) both state that it will throw a NullPointerException if the format string is null.

Is it the case that with only 1 string passed in, the JVM considers the string already formatted?

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

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

Post by admin »

Acutally, the first argument to printf is the format string. So here, it is not the format string that is null but the arg parameter that is null. So there is no reason for it to throw NPE.
It will not write anything to the stream though (because there is no argument to write).

It would be better if changed to pw.printf("%s", text)

HTH,
Paul.
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 239 guests