What is a stream?

Before we come to the actual definition of stream let us first consider an example in terms of operating system. We can have multiple applications running on our system. In order to identify each application an OS makes use of port numbers. When we send some data from one port to another then we are forming a stream between the two. So stream here is the direction or we can also say a combination of source port and destination port.

Consider an example: suppose we want to execute a java application. To do that we first start command prompt. So the OS assigns some port number P1 to it. Now to compile our java application we use javac.exe which is the compiler for java and OS assigns port number say P2 to it. But compiling is a small process so it will finish early. Once the application is compiled we launch our application using java interpreter i.e. java.exe. OS assigns it a port number say P3. According to the feature of JRE, it creates two static objects: in and out, identifying the direction from P1 to P3 and from P3 to P1 respectively. Both these objects have been declared in java.lang.System class.

Therefore whenever we want to take some input into our application we have to specify the direction using System.in and when we want to send some output from our application we have to specify its direction using System.out. There are mainly two streams in java: byte stream and character stream. Detailed discussion on both the streams would be done in next tutorial.