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