Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum size of characters when using buffered reader readline() from System.in

Tags:

java

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)

like image 736
theorist17 Avatar asked Oct 17 '22 12:10

theorist17


1 Answers

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.

like image 85
Ulf Jaehrig Avatar answered Nov 15 '22 05:11

Ulf Jaehrig