Java 什么是system.in

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24786399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:50:35  来源:igfitidea点击:

What is system.in

java

提问by user3762764

Consider this Scannerinput example.

考虑这个Scanner输入示例。

Scanner user_input = new Scanner( System.in );

Here Scanneris the CLASS. user_inputis the OBJECT under Scanner class. What is (System.in)? Is it a parameter passed or a object under Scannerclass?.

这里Scanner是类。 user_input是 Scanner 类下的 OBJECT。什么是(System.in)?是传递的参数还是Scanner类下的对象?

Consider another example.

考虑另一个例子。

dog dog1 = new dog(25)

Here I have set dog class to accept size as a parameter.

在这里,我已将狗类设置为接受大小作为参数。

What exactly is System.in?

究竟是System.in什么?

回答by ltalhouarne

System.inis the "standard" input stream.

System.in是“标准”输入流。

Take a look at the following documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html

看看下面的文档:http: //docs.oracle.com/javase/7/docs/api/java/lang/System.html

回答by Suresh Atta

Scanner class accepts input stream as a parameter and System class have a static variable inwhich is of type InputStream. System.ingives you a instance of of type InputStream.

Scanner 类接受输入流作为参数,而 System 类有一个in类型为 的静态变量InputStreamSystem.in给你一个 type 的实例InputStream

Check this doc of public static final InputStream in

检查这个 doc of public static final InputStream in

The "standard" input stream. This stream is already open and ready to supply input data.

“标准”输入流。此流已打开并准备好提供输入数据。

回答by Suresh Atta

From the source:

来源

System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from Java IO.

System.in 是一个 InputStream,它通常连接到控制台程序的键盘输入。System.in 并不经常使用,因为数据通常通过命令行参数或配置文件传递到命令行 Java 应用程序。在具有 GUI 的应用程序中,应用程序的输入是通过 GUI 提供的。这是一种独立于 Java IO 的输入机制。

回答by Praveen Chaurasiya

Scanner class has a constructor Scanner(InputStream) in its profile i.e when we call Scanner() constructor during the object creation time it will let you to pass the object of InputStream class.

Scanner 类在其配置文件中有一个构造函数 Scanner(InputStream) ,即当我们在对象创建期间调用 Scanner() 构造函数时,它将让您传递 InputStream 类的对象。

"in" is an object of "InputStream" class defined in System class(like "out" is an object of PrintStream class defined in System class).

“in”是System类中定义的“InputStream”类的对象(如“out”是System类中定义的PrintStream类的对象)。

Thus System.in is nothing but calling the "in" object of InputStream class defined in System class when Scanner constructor is called during object creation time.

因此 System.in 只不过是在对象创建期间调用 Scanner 构造函数时调用 System 类中定义的 InputStream 类的“in”对象。

Scanner(InputStream) is pre-defined constructor in Scanner class which when calls requires the object of InputStream class as the parameter and that object is System.in.

Scanner(InputStream) 是 Scanner 类中预定义的构造函数,调用时需要 InputStream 类的对象作为参数,该对象为 System.in。

回答by coders hub

System.inis used to get input from the user. Consider the following example:

System.in用于获取用户的输入。考虑以下示例:

import java.util.Scanner;

class Method {
    public static void main(String args[]) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Name");
        String s=sc.next();
        System.out.println("Enter roll number");
        int r=sc.nextInt();
        System.out.println("Enter marks");
        float t=sc.nextFloat();
        System.out.println("Name:" + s);
        System.out.println("Roll No.:" + r);
        System.out.println("Marks:" + t);
    }
}

Output:

输出:

Enter Name

Roshal

Enter roll number

204

Enter marks

65

Name:Roshal

Roll No.:204

Marks:65.0

输入名字

罗沙尔

输入卷号

204

输入标记

65

姓名:罗沙尔

卷号:204

分数:65.0

For explanation of this code visit: Accepting input in java

有关此代码的说明,请访问: Accepting input in java

回答by Vinita Tyagi

System.inin java means to take input from the keyboard or user. System.outin java means to print the output to console.

System.in在 java 中意味着从键盘或用户获取输入。 System.out在 java 中意味着将输出打印到控制台。

回答by Eric H.

The Scanner constructor's parameter System.inreferences the static InputStream field indefined in the final System class. As a static field inrequires its class identifier.

Scanner 构造函数的参数System.in引用了最终 System 类定义的静态 InputStream 字段。作为静态字段in需要其类标识符。

Oracle states that inis the "standard" input stream1, not that System.in is. The quotes around standard likely refer to the irony that inputting from a keyboard using a command line interface is no longer thestandard, what with them newfangled GUIs and all.

Oracle 声明in是“标准”输入流1,而不是 System.in 。标准周围的引号可能是指具有讽刺意味的是,使用命令行界面从键盘输入不再标准,新奇的 GUI 等等。