What is the exact difference between out.write() and out.print()

后端 未结 9 608
情深已故
情深已故 2020-12-13 20:49

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

相关标签:
9条回答
  • 2020-12-13 21:26

    I simply know it as like this:

    out.println() is method of javax.​servlet.​jsp.​JspWriter

    out.write() is method of java.io.Writer

    0 讨论(0)
  • 2020-12-13 21:29

    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. 
    
    0 讨论(0)
  • 2020-12-13 21:31

    out.write(-) vs out.print(-)

    One more difference is out.write(-) method just write data or object to browser like a file. You can not write any statement like out.write(10*20); but we do this with out.print(10*20);

    0 讨论(0)
  • 2020-12-13 21:33

    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
    
    0 讨论(0)
  • The out variable in your case is most likely refers to a PrintWriter

    Just compare the description of write...

    public void write(String s)

    Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.

    ... with the description of println ...

    public void println(String x)

    Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

    ... and print ...

    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.

    All in all I'd say that the print methods work on a higher level of abstraction and is the one I prefer to work with when writing servlets.

    0 讨论(0)
  • 2020-12-13 21:36

    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.out.print('j');
    

    first statement writes character 97 i.e 'a' on console but does not print, while second statement prints 'a' which is written already on stream and 'j' which is passed in print() method.

    0 讨论(0)
提交回复
热议问题