简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM