What is the type of System.out in Java?
Asked Answered
C

4

5

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?

Craigcraighead answered 21/1, 2013 at 8:47 Comment(3)
You can use java's reflection to check all important object information at runtime.Brachiate
Try System.out.println(System.out.getClass()); or view it in your debugger. ;)Seagraves
@PeterLawrey this will print class java.io.PrintStream nothing else :) The idea with debugger is more efficient ;)Gymnasiarch
G
3

PrintStream wraps BufferedOutputStream, which wraps FileOutputStream, which is writing into the console, which has its own FileDescriptor.

Gymnasiarch answered 21/1, 2013 at 8:51 Comment(4)
I'd like to add that the static field System.out does not have to be instantiatied by the user. It can be just used. My feeling is that the OP is having some trouble with understanding that.Pool
@Pool I have not that feeling. It seems to me, that it is just theoretical question.Gymnasiarch
Technically it is a FileDescriptor. The File class is not used.Seagraves
@PeterLawrey Thanks, its my misprint. I'll fix it.Gymnasiarch
S
4

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

  • 0 is System.in
  • 1 is System.out
  • 2 is System.err
  • 3+ is used for other files

enter image description here

Seagraves answered 21/1, 2013 at 9:3 Comment(3)
FileInputStream for stdin, FileOutputStream for stdout. What about stderrInsouciance
@Insouciance stderr is much the same as stdout.Seagraves
Correct. Can you also comment on first part of query? about the idea of streamInsouciance
G
3

PrintStream wraps BufferedOutputStream, which wraps FileOutputStream, which is writing into the console, which has its own FileDescriptor.

Gymnasiarch answered 21/1, 2013 at 8:51 Comment(4)
I'd like to add that the static field System.out does not have to be instantiatied by the user. It can be just used. My feeling is that the OP is having some trouble with understanding that.Pool
@Pool I have not that feeling. It seems to me, that it is just theoretical question.Gymnasiarch
Technically it is a FileDescriptor. The File class is not used.Seagraves
@PeterLawrey Thanks, its my misprint. I'll fix it.Gymnasiarch
C
1

Yes out is of PrintStream type. And constructor of PrintStream takes OutputStream type. OutputStream is abstract class. But any superclass refrence can refer subclass object without casting, so PrintStream's constructor has OutputStream refrence, but this refrence must be referring one of OutputStream's subclass like FileOutputStream

Cence answered 21/1, 2013 at 9:2 Comment(0)
S
1

There are a couple more things to say about the implementation of System.out.

  1. 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.

  2. The System::setOut(PrintStream) method can be used to modify what System.out is bound to. If that happens, any assumptions about implementation classes may be incorrect.

  3. It turns out that you can do this:

      System.setOut(null);
    

    so System.out could be null. However, with current implementations, System.out won't be null unless you set it to null.

All that is actually guaranteed by the specifications is that the value of System.out will have a type that is assignment compatible with PrintStream.

Silk answered 26/10, 2017 at 11:45 Comment(2)
But we know the implementation class is either PrintStream or a subclass of PrintStream, right?Subclinical
Yes. That's more or less what "assignment compatible" means! (But ... quibble ... null does not have an implementation class. Which is why "assignment compatible" is technically more accurate than what you said.)Silk

© 2022 - 2024 — McMap. All rights reserved.