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

后端 未结 9 632
情深已故
情深已故 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: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. 
    

提交回复
热议问题