condinya
condinya

Reputation: 988

java console input

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????

Upvotes: 2

Views: 3481

Answers (2)

Catchwa
Catchwa

Reputation: 5855

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));

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

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

Upvotes: 5

exodrifter
exodrifter

Reputation: 658

In java you can do:

Scanner scan = new Scanner(System.in);
scan.nextInt();
scan.nextDouble();

etc. You just need to make sure the next input is correct.

EDIT: Missed the Buffered Reader part. I think this answer is totally irrevelant.

Upvotes: 3

Related Questions