2

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

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

3 Comments

i mean to ask why input is of String data type by default....why not other.what is the benefit of java style input over C where we have facility to input all primitive data types
@Bipul Like webdestroya said before me, that's just how java works. I'm pretty sure that when you enter in the values in the command prompt, java recognizes those as Strings instead of primitives for the sake of keeping it simple rather than trying to figure out what it is before trying to scan it in. Java leaves it up to the user to know what the input is. Sorry about the unsatisfactory reply, since I'm not very sure about what I just said.
i agree with u that java does it to make things simpler and leaves it to user to decide.Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.