开发者

Output Stream in java

Please explain out variable in System.out

out is sometimes referred to be object of type OutputStream sometimes referred to as object of type PrintStream开发者_运维百科

and even when it is predefined variable, it is sometimes assigned it to PrintWriter object

PrintWriter out= response.getWriter();

is it due to the fact that a superclass reference can be assigned the reference to objects of its subclass?


Super class reference variable can hold the reference of sub-class object. The OutputStream is an abstract super class of all OutputStream of bytes classes, so you can say the System.out field as OutputStream type.


PrintStream is a subclass of OutputStream, and System.out is a PrintStream, so it truly is both.

The line:

PrintWriter out= response.getWriter();

has nothing to do with System.out. I don't know where that line of code is from. It's defining a local variable named out that's totally independent of System.


To the best of my knowledge, in System.out, "out" is a name of a method. When you say System.out.print() you call the System class and its out() method. This out() is a static method which gives you the reference to "System" class object. So after you get the reference you call the print() method.

But in, PrintWriter out= response.getWriter(); you are just creating a reference variable of PrintWriter class. So as my fellow colleagues mention, there is no connection between "System.out" and "PrintWriter out". There are two purposes for those two.

Correct me if I'm wrong. Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜