日期:2014-05-20  浏览次数:20676 次

关于System.in.read()
import java.io.IOException;

public class Test {

public static void main(String[] args) {

int s = 0;
try {
s = System.in.read();
} catch (IOException e) {
e.printStackTrace();

System.out.println(s);

}

}


就这样一个程序,比如输入1,打印出49
说明是按char的方式对待输入

但是看System.in

/**
 * The "standard" input stream. 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.
 */
public final static InputStream in = nullInputStream();

/**
 * The following two methods exist because in, out, and err must be
 * initialized to null.  The compiler, however, cannot be permitted to
 * inline access to them, since they are later set to more sensible values
 * by initializeSystemClass().
 */
private static InputStream nullInputStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}


一个被赋值为null的final型变量是如何调用read方法的?还有和char是如何关联起来的




------解决方案--------------------
来学习的~~~~
------解决方案--------------------
引用:
Java code?1234567891011121314151617import java.io.IOException; public class Test {     public static void main(String[] args) {         int s = 0;        try {            s = System.in.re……


read()方法应该是按照byte的方式来对待输入输出的。

至于第二个问题通过查阅api,应该是在System类中进行初始化
之后调用read()方法的。看看它所描述的信息吧
public final class System {

    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();