简体   繁体   English

System.in和Scanner对象

[英]System.in and Scanner object

I am having a problem to understand System.in specific job all what i understand is that it's a data field in the System class , and it's declaration is 我在了解System.In的特定工作时遇到了一个问题,我所了解的是这是System类中的一个数据字段,并且它的声明是

public static final InputStream in

It's declared final so it cannot be changed , as mentioned in the Oracle source they say that " in " is the "standard" input stream. 它被声明为final,因此无法更改,正如Oracle资料中提到的那样,他们说“ in”是“标准”输入流。 This stream is already open and ready to supply input data. 该流已经打开,可以提供输入数据了。 Typically this stream corresponds to keyboard input or another input source specified by the host environment or user. 通常,此流对应于键盘输入或主机环境或用户指定的另一个输入源。

what does this exactly means ? 这到底是什么意思? .

when i write the following code : 当我编写以下代码时:

Scanner input = new Scanner(System.in);

that Scanner object is ready to scan what's in System.in , but nothing shows up on the CMD 该Scanner对象已准备好扫描System.in中的内容,但是CMD上没有任何显示

another point is that when i try to print System.in i end up with an output like so 另一点是,当我尝试打印System.in时,最终输出如下

java.io.BufferedInputStream@15db9742

Can anyone please explain the whole process and what's the exact job of both System.in and Scanner class ! 谁能解释一下整个过程以及System.in和Scanner类的确切工作!

System.in is a BufferedInputStream , such a Stream can be any input stream (for instance a network socket, or a file), but also the standard input channel stdin . System.inBufferedInputStream ,这样的Stream可以是任何输入流(例如,网络套接字或文件),也可以是标准输入通道stdin

stdin is the content you type in the command line, so obviously, it doesn't show anything. stdin是您在命令行中键入的内容,因此很明显,它不显示任何内容。

When you create a Scanner as shown in your question, you feed the standard input channel to that Scanner , but the scanner won't do anything until it is asked to. 当您按照问题所示创建Scanner ,您会将标准输入通道送入该Scanner ,但是直到被要求Scanner ,扫描仪才做任何事情。 A scanner is a wrapper : a construct that makes it easier (more convenient) to parse input from the stdin . 扫描器是包装器 :一种构造(使解析stdin输入更加容易(更方便))。 It does not offer additional functionality in the sense you can do all what a scanner does by yourself, but it is way easier to use a scanner if you want to parse for instance integers from the stdin . 从某种意义上说,它没有提供其他功能,您可以自己完成扫描程序的全部工作,但是如果您要从stdin 解析整数,使用扫描程序会更容易。

So when you for instance type: 因此,当您输入实例时:

Scanner sc = new Scanner(System.in);
int val = sc.nextInt();
System.out.println(2*val);

It will wait until you input a line, parse it to an integer and print the double of the inputted value on the standard output channel ( stdout ). 它将等到您输入一行,将其解析为整数,然后在标准输出通道( stdout )上打印输入值的两倍。


A few concluding notes: 一些结论性注释:

System.in is not final , you can use System.setIn to set another input stream as the standard input string. System.in不是final ,可以使用System.setIn将另一个输入流设置为标准输入字符串。 For instance a file or a network socket. 例如文件或网络套接字。 The default System.in can also take input from for instance a file or a pipe if you for instance call the program as java -jar program.jar < inputfile . 如果您将程序调用为java -jar program.jar < inputfile ,则默认的System.in也可以从文件或管道中获取输入。

Scanner is a class in java.util package . Scannerjava.util包中的类。

When you write Scanner sc=new Scanner(System.in) you are trying to read the input from the "standard" input stream . 当您编写Scanner sc=new Scanner(System.in)您试图从"standard" input stream读取"standard" input stream Mainly System.in is used for reading the input from the console 主要是System.in用于从控制台读取输入

Scanner holds the program execution until the input arrives, that is how it is meant to be. 扫描程序会保持程序执行,直到输入到达为止,这就是本来的意思。

And your question that when you print the System.in you end end up with a output like java.io.BufferedInputStream@15db9742 , this output is called as the object hash code and you are likely to get this kind of output if you try to print an object. 您的问题是,在打印System.in时最终会得到类似java.io.BufferedInputStream@15db9742的输出,该输出称为对象hash code ,如果尝试执行以下操作,则很可能会得到这种输出打印对象。 Here you are getting the hash code because you are trying to print an entire stream. 在这里,您将获得哈希码,因为您正在尝试打印整个流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM