user961690
user961690

Reputation: 778

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?

Upvotes: 1

Views: 183

Answers (3)

Thilok Gunawardena
Thilok Gunawardena

Reputation: 924

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.

Upvotes: 1

Ed Staub
Ed Staub

Reputation: 15690

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.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94645

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.

Upvotes: 2

Related Questions