String character limit

I am developing an application that sends a String over a socket, but I have not set a limit on the size of the string. Is there a limit to the size of a string in characters or bytes in java? What about sending a very large string (millions of characters) over a socket? Is there a limit?

+3


source to share


4 answers


The number of characters in a string is determined by the maximum size of an array in Java. This is Integer.MAX_VALUE (2 147 483 647). You will most likely run out of RAM before you hit that limit.

There is no limit to the number of characters that can be sent over the Socket.



If your data structure allows you, you may be given the option to work with the streaming API. for example if a string is being output from the db or from disk it runs it on a socket when you read it into memory. This way you don't need to hold the entire string in memory.

+1


source


Yes. The maximum string size is the maximum number of characters that the underlying char array can accept, which is Integer.MAX_VALUE

.



+1


source


Maximum string length Integer.MAX_INT = 2 147 483 647

. This assumes you have enough memory to store String

this size, of course.

+1


source


You can get a string of length Integer.MAX_VALUE (always 2147483647 (2 ^ 31 - 1) as per Java spec, the maximum array size the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), into whichever is less.

Source

+1


source







All Articles