This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How System.in works without any read()?  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think so, yes.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Wanna see my flashlight? How about this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!