PrintWriter Question

D

Danger_Duck

Continuing my vein of simple java questions...For the
Java.io.PrintWriter, what is the difference between write and print?

For me, print and write mean the same thing so the difference between:

print(char[] s)
Print an array of characters.

and

write(char[] buf)
Write an array of characters.

is unknown. This is made especially acute when the documentation says
things like:
"print

public void print(char c)

Print a character. The character is translated into one or more
bytes according to the platform's default character encoding, and
these bytes are written in exactly the manner of the write(int)
method."

Anyone care to enlighten me on the difference and recommend when to
use which method?
Thanks!
 
J

Joshua Cranmer

Danger_Duck said:
Continuing my vein of simple java questions...For the
Java.io.PrintWriter, what is the difference between write and print?

The write methods are overridden from java.io.Writer. The print methods
are defined in java.io.PrintWriter, most likely retained for consistence
with java.io.PrintStream.

So write(char[]) == print(char[]), but the latter should probably be
preferred.
 
A

Andreas Leitgeb

Danger_Duck said:
public void print(char c)

Print a character. The character is translated into one or more
bytes according to the platform's default character encoding, and
these bytes are written in exactly the manner of the write(int)
method."

int i=42;
PrintWriter pw= ...;
pw.print(i); // --> "42"
pw.write(i); // --> "*" (ascii 42)

It's like they differ by some ".toString()" conversion.
For "char" and "String"(unless null), there's no visible
difference. "print" is generally more comfortable to use.

You can lookup the base-class'es docu for the write-methods.
The PrintWriter's versions of them are just wrappers to hide
IOExceptions.
 
M

Mark Space

Danger_Duck said:
Continuing my vein of simple java questions...For the
Java.io.PrintWriter, what is the difference between write and print?

class PrintTest {
public static void main( String ... args ) {
char [] c = { 42 };
PrintWriter pw = new PrintWriter( new OutputStreamWriter(
System.out ) );
pw.print( c );
pw.write( c );
pw.flush();
}
}

compile:
run:
**
BUILD SUCCESSFUL (total time: 0 seconds)

Both print '*', so I dunno. Possibly whoever extended the Writer class
to make the PrintWriter class felt there should be methods named "print"
not "write" so they just added them, even if there's no functional
difference.
 
M

Mike Schilling

Mark said:
Danger_Duck said:
Continuing my vein of simple java questions...For the
Java.io.PrintWriter, what is the difference between write and print?

class PrintTest {
public static void main( String ... args ) {
char [] c = { 42 };
PrintWriter pw = new PrintWriter( new OutputStreamWriter(
System.out ) );
pw.print( c );
pw.write( c );
pw.flush();
}
}

compile:
run:
**
BUILD SUCCESSFUL (total time: 0 seconds)

Both print '*', so I dunno. Possibly whoever extended the Writer
class to make the PrintWriter class felt there should be methods
named "print" not "write" so they just added them, even if there's no
functional difference.

The print() methods are not declared as throwing IOExceptions, which the
write() methods are. In fact, neither of them actually does (an IOException
sets an error flag which can be checked with checkError(), but then returns
normally. This makes the print() methods more convenient to use when
IOExceptions needn't be handled immediately, e.g. when writing to stdout.
 
A

Andreas Leitgeb

.... This makes the print() methods more convenient to use when
IOExceptions needn't be handled immediately, e.g. when writing to stdout.

There *is* a functional difference between write and print, except
for the char, char[] and String overloads, which appear to be the
same except for null-values.

Try to call each with an "int" argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top