2

I want to know where is the difference between System.out.write and System.out.print in my Java class.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
fredy
  • 29
  • 1
  • 2
  • 1
    It 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 Answers1

7

The two methods of PrintStream have different meanings:

  • print(int) writes a decimal representation of the entire int, while
  • write(int) writes the least significant byte of the specified int 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.

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523