java控制台输入 [英] java console input

查看:487
本文介绍了java控制台输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过控制台输入的任何数据类型(就像我使用BufferedReader类一样)是String.After之后我们输入它来重新排列数据类型(作为整数的Inter.parseInt())。但是在C中我们可以采取任何原始数据类型的输入,而在java中,所有输入类型都是String.why它是如此????

The data type of the any input through console (as i do using BufferedReader class) is String.After that we type cast it to requered data type(as Inter.parseInt() for integer).But in C we can take input of any primitive data type whereas in java all input types are neccerily String.why it is so????

推荐答案

控制台输入实际上是作为一系列字节读入的,而不是字符串。这是因为API将 System.in 公开为 InputStream 。在JDK1.5之前的典型包装(对于 Scanner 类来说很好!)类似于:

Console input is actually read in as a series of bytes, not a String. This is because System.in is exposed by the API as an InputStream. The typical wrapping before JDK1.5 (hooray for the Scanner class!) was something like:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

ie InputStreamReader 将字节流转换为字符流,然后 BufferedReader 用于执行 readLine()操作,或者其他什么。

i.e. InputStreamReader converts the byte stream into a character stream and then the BufferedReader is used to do your readLine() operations, or whatever.

所以这是一个 String 输出,因为你'从 BufferedReader 获取字符流的缓冲输出。

So it's a String output because you're getting the buffered output of a character stream from your BufferedReader.

这篇关于java控制台输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆