I want to know where is the difference between System.out.write
and System.out.print
in my Java class.
Asked
Active
Viewed 9,261 times
2

Willi Mentzel
- 27,862
- 20
- 113
- 121

fredy
- 29
- 1
- 2
-
1It is actually no big difference, you inherit the write from the underlying output stream. You can use both but print/printf is typically more convinient (especially as it supresses exceptions). – eckes Feb 15 '15 at 01:40
-
For me I used System.out.write() but it didnt work,I change it to println and it works.I still didnt understand why. – Kayode Apr 15 '17 at 09:40
1 Answers
7
The two methods of PrintStream
have different meanings:
print(int)
writes a decimal representation of the entireint
, whilewrite(int)
writes the least significant byte of the specifiedint
to the output.
This leads to different results: if you call print(48)
, the output is going to be 48
, but if you call write(48)
, the output would be system-dependent, but on most systems it would be 0
.

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523
-
1
-
@immibis When you work with characters, it does. But the `write` call is on the byte level, not on the character level, so the system is free to interpret that byte in a system-specific way. – Sergey Kalinichenko Feb 15 '15 at 02:16
-