In my servlet I gave both out.print
and out.write
. but both prints in the browser.
What is the exact difference between these two and when to use out.print
and out.write
?
The only difference between println and print method is that println throws the cursor to the next line after printing the desired result whereas print method keeps the cursor on the same line.
write() method only writes characters to stream(or console) but does not print, while print() method writes and print it on stream (or console). System. out. write(97); System.
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.
By using PrintWriter than using System. out. println is preferred when we have to print a lot of items as PrintWriter is faster than the other to print data to the console.
The basic difference is that out.write()
explodes if you pass it a null:
String s = null;
out.print(s); // outputs the text "null"
out.write(s); // NullPointerException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With