This question already has an answer here:
What is the difference between write()
and print()
methods in PrintWriter
class in Java?
This question already has an answer here:
What is the difference between write()
and print()
methods in PrintWriter
class in Java?
print() formats the output, while write() just prints the characters it is given. print() handles many argument types, converting them into printable strings of characters with String.valueOf(), while write() just handles single characters, arrays of characters, and strings.
To illustrate the difference, write(int) interprets the argument as a single character to be printed, while print(int) converts the integer into a character string. write(49) prints a "1", while print(49) prints "49".
source: http://www.coderanch.com/t/398792/java/java/write-print
print() formats the output, while write() just prints the characters it is given. print() handles many argument types
According to coderanch and this one too
write() is supposed to be used when you need to just print characters while print() is supported to be used when you need to format character output.
write(int)
writes a single character (hence takes the Unicode character of the passed int
).
print(datatype)
on the other hand converts the datatype (int
, char
, etc) to a String by first calling String.valueOf(int)
or String.valueOf(char)
which is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int)
method.
For more details, you can refer the documentation of PrintWriter
.
public void write(String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.supports only int and String as parameters
print method has higher level of abstraction.
public void print(String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.supports all primitive data types
check this