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

I just want to know how many characters I can put in when I use the above statement. For example, if I can put in "aaaaa" or "abcde" into console, that would mean I can put 5 or more characters. Then can I put in "a" 2,147,483,647 times? (max value of integer) System.in seems like internally storing the sequence of input as interger type.

If it is true, how many 2-byte characters can I put in? Is it 1,073,741,823? (the half of that number)

Hulk
  • 6,399
  • 1
  • 30
  • 52
theorist17
  • 55
  • 6
  • And what would happen if the input was longer than the maximum length? Would there will be an error? or would JVM cut off the text part from the point where it's too long? :) – theorist17 Sep 26 '18 at 14:11

1 Answers1

3

The maximum should not be limited by System.in, as it should block if it can't accept more characters, but by the readLine() method. If you take a look into the implementation of readLine(), you will see that the result is built via a StringBuffer. This holds the string to built in a char array. So the maximum number of characters to be returned is limited by the maximum length of a char array. (Which is Integer.MAX_VALUE - 5 according to: Do Java arrays have a maximum size?). If you add more characters, you will get an OOM.

Ulf Jaehrig
  • 749
  • 5
  • 11