2

It is known fact that System.in and System.out is a InputStream and PrintStream respectively. Thus it is a java.io Streams.

When we work with streams we should open and close its. Also we should to catch a lot of exceptions. When we work with System.in and System.out we spared from these activities.

Who executes these activities instead of us? and when(under what conditions?) streams opens/close ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

2

The Java runtime ("interpreter") itself is responsible for opening those handles before your code starts running.

So they're available as soon as you enter main(). Unless you close them yourself, they'll stay open for the duration of the program.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • and it is the OS before that; there are setters on System where the Java Runtime is setting instances of InputStream and PrintStream that wrap the underlying OS level streams. Unless ofcourse one goes and calls those setters themselves :) – Chris K Sep 03 '14 at 09:20
  • And as the JVM opened them, it can be argued that [only the JVM should close them](http://stackoverflow.com/a/7457737/545127). – Raedwald Jun 22 '15 at 11:52
  • 1
    @Raedwald, I wasn't suggesting they be closed, though it's certainly valid to do so under some circumstances. I was merely providing a complete answer to the question 'when(under what conditions?) streams opens/close ?'. – paxdiablo Jun 23 '15 at 07:28