What is the exact difference between out.write() a

2020-01-30 11:35发布

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 ?

9条回答
Explosion°爆炸
2楼-- · 2020-01-30 11:54

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. 
查看更多
Lonely孤独者°
3楼-- · 2020-01-30 11:55

First thing is you can't use javax.​servlet.​jsp.​JspWriter out in a servlet. It has to be used in a .jsp file, because out is a method local variable in _jspService(...) method of your .jsp file.

There is no difference in the purpose of using out.print() and out.write(). Both are used to write the String version of the given object to JspWriter's buffer.

However, JspWriter.print() is capable of taking many types of arguments than Writer.write().

JspWriter.print()

  • Object
  • String
  • boolean
  • char
  • char[]
  • double
  • float
  • int
  • long

Writer.write()

  • String
  • char
  • int
查看更多
beautiful°
4楼-- · 2020-01-30 11:57

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
查看更多
\"骚年 ilove
5楼-- · 2020-01-30 11:59

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.

查看更多
一纸荒年 Trace。
6楼-- · 2020-01-30 12:09

PrintWriter's implementation communicates the difference better than javadoc

public void print(String s) {
    if (s == null) {
        s = "null";
    }
    write(s);
}
查看更多
可以哭但决不认输i
7楼-- · 2020-01-30 12:10

PrintWriter:

public void write(String s)

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

print method has higher level of abstraction.

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.

Hope this helps.

查看更多
登录 后发表回答