aspose file tools*
The moose likes Beginning Java and the fly likes How System.in works without any read()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How System.in works without any read()?" Watch "How System.in works without any read()?" New topic
Author

How System.in works without any read()?

isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
HI all,

I was trying with programs to read User Input in Java. We have System.in to get User Input. When i read about System Class..its a Final class where 'in' is the Object of InputStream Class. Now if we have to read certain data,we should use some read() function as we use print() in PrintStream() to write on Console.

Also InputStream is an abstract class whose explanation is written on Java Api:

"Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input."

So my question is that how (System.in) passed an argument read chars from console without any use of read() method in its signature?

Thanks!!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
System.in is a reference to an instance of a class that extends InputStream. This class will implement the read() method. What the actual class is doesn't matter (and could be different for different versions of Java). All you need to know is that it extends InputStream.


Joanne
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50

Scanner class here takes (system.in) as argument or we say reference of InputStream. Thus its the responsibility of scanner class to read the data from console?am i correct?



Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I think so, yes.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

isha krishnan wrote:Scanner class here takes (system.in) as argument or we say reference of InputStream. Thus its the responsibility of scanner class to read the data from console? am i correct?

Not quite. System.in does the actual reading, Scanner just packages those reads in a friendlier (??? ) form.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Hi Winston,
SO my question is now,how (System .in) read.Where is it written that it has to take input from Console window?How it connects with console.

I guess now my question is puzzling me.Its more like now how computer works?There would be some logic written that if a reference of inputstream is generated,read from console.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
isha krishnan wrote:Where is it written that it has to take input from Console window?How it connects with console.

The JVM automatically connects System.in to the operating system's standard input when it is started up. How it does this (and what the operating system's standard input is connected to) is operating system dependent.

isha krishnan wrote:There would be some logic written that if a reference of inputstream is generated,read from console.

No. Only System.in automatically reads from the console (or whatever the OS's standard input stream is connected to). You can create other InputStreams in your code that can connect to files, i/o devices, etc.
 
 
subject: How System.in works without any read()?