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
Answer from Maantje on Stack Overflowprint() 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
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.
java - What is the exact difference between out.write() and out.print() - Stack Overflow
java - PrintWriter print vs println - Stack Overflow
Difference between `write` and `print`
PrintWriter System.out differences...?
Videos
The short answer 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
The more complete answer is that out in servlets is a PrintWriter whose overloaded write() methods only accept a few basic types but do the work of outputting bytes to the underlying OutputStream.
The print() method is a convenience wrapper method that wraps calls to write() to provide custom behaviour for its numerous overloaded implementations. For example:
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
public void print(Object obj) {
write(String.valueOf(obj));
}
public void print(double d) {
write(String.valueOf(d));
}
etc for all the primitive types.
There are three major differences:
1) If you try to print a null value of a String with out.write() , It will throw NullPointerException while out.print() will simply print NULL as a string.
String name = null;
out.write(name); // NullPointerException
out.print(name); // 'Null' as text will be printed
2) out.print() can print Boolean values but out.write() can not.
boolean b = true;
out.write(b); // Compilation error
out.print(b); // 'true' will be printed
3) If you are using out.write(), you simply can not place arithmetic operation code but out.print() provides the support.
out.write(10+20); // No output will be displayed.
out.print(10+20); // Output '30' will be displayed.
So I made some code to test a theory, and it disproved my theory. Here it is:
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
PrintWriter writer = new PrintWriter(System.out);
System.out.println("HI");
writer.println("HI");
writer.flush();
}
}Now here's the context for my question. I was taught that System.out is a stream object, and that the console grabs from the System.out stream and puts those contents on its stream. I was then taught that PrintWriter is an class that functions essentially identically to System.out, except that you can direct your destination stream. So I hypothesized, "If you made a PrintWriter object and sent the System.out stream in as its destination, would that be identical to just using System.out?
As it turns out, no, but I still don't fully understand why. What I do understand is that if I use the flush() method, it does essentially work the same. So my question is this:
When you use System.out.println() , does the method println() also tell the console to grab from the buffer so its not left in the System.out stream? And from the other perspective, when you use writer.println() (writer being the PrintWriter object in this context), does the println() method NOT tell the destination stream to grab from the buffer (until the flush() method is used)?
Any information helps, feel free to tell me that my interpretation is wrong or that my hypothesis is wrong. Or you can tell me anything really, tell me how your day is going. Any information helps, the more I know the better a programmer I can become :)