I am just a newbie in Java. I was wondering the way System.out.println()
is used. Out is a static field inside System
class. The type of out
is PrintStream
. But when I saw the constructor of PrintStream
class, it takes a parameter of type OutputStream
and as far as I know we cannot create the object of an abstract class. In that case we must pass some subclass's object to the constructor of PrintStream
. What is that class? Same is the System.in
. It is also InputStream
's reference but what is the type of object it points to as the InputStream
is abstract?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
PrintStream
wrapsBufferedOutputStream
, which wrapsFileOutputStream
, which is writing into the console, which has its ownFileDescriptor
.Yes
out
is ofPrintStream
type. And constructor ofPrintStream
takesOutputStream
type.OutputStream
is abstract class. But any superclass refrence can refer subclass object without casting, so PrintStream's constructor hasOutputStream
refrence, but this refrence must be referring one ofOutputStream
's subclass likeFileOutputStream
There are a couple more things to say about the implementation of
System.out
.The actual implementation class of
System.out
is not specified. The javadocs don't say what it is. We observe (in various way) that Oracle Java and OpenJDK Java implement the "stack" in a particular way (see other answers), but this could change in the future.The
System::setOut(PrintStream)
method can be used to modify whatSystem.out
is bound to. If that happens, any assumptions about implementation classes may be incorrect.It turns out that you can do this:
so
System.out
could benull
. However, with current implementations,System.out
won't benull
unless you set it tonull
.All that is actually guaranteed by the specifications is that the value of
System.out
will have a type that is assignment compatible withPrintStream
.A simple way to view the structure of a class is to examine it in a debugger.
As you can see @Andremonify's description is basically what you have.
FileDescriptor