What is the difference between write()
and print()
methods in PrintWriter
class in Java?

- 1,972
- 2
- 18
- 33

- 4,412
- 10
- 51
- 74
-
2http://www.coderanch.com/t/398792/java/java/write-print – Coding Enthusiast Jun 05 '15 at 09:13
-
I guess there is not much difference, as behind the scene `print()` method calls `write()` method. For example: `public void print(int i) { write(String.valueOf(i)); }` – Kartic Jun 05 '15 at 09:32
-
@Kartic - You guessed wrong :-) The `valueOf` call is *formatting* the integer. With `write(int)` you are writing a character represented as an `int`. – Stephen C Feb 21 '19 at 15:03
5 Answers
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

- 3,865
- 1
- 27
- 50

- 1,781
- 2
- 20
- 33
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
.

- 1,972
- 2
- 18
- 33
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

- 3,865
- 1
- 27
- 50
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.

- 31
- 1
- 2
- 4
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
-
1Also, whereas `write()` supports only `int` and `String` as parameters, `print` supports all primitive datatypes. – Anindya Dutta Jun 05 '15 at 09:21