2 ms·
I wrote some stress-test cases for a Java library recently which involved 1-GB and 2-GB strings. On the surface, a java.lang.String should be a wrapper around
by nayuki 4y ago
I wrote some stress-test cases for a Java library recently which involved 1-GB and 2-GB strings.
On the surface, a java.lang.String should be a wrapper around a char[], whose maximum length is Integer.MAX_VALUE = 2 147 483 647. This is roughly how older implementations of JDK did things.
But as of JDK 9 and JEP 254, String's private field has type byte[], and each character uses either 1 byte if it's encodable in ISO 8859-1 or 2 bytes if it requires UTF-16.
This means that strings containing only ASCII characters can be up to about 2 billion in length, whereas strings with real Unicode content can only be up to about 1 billion in length. This is a bit of an unfortunate regression in functionality.
- ungamedplayer 4y agoNaive question, why not just do the java equivalent of mmap and read the data from storage? Seems like somewhere along the line reality outgrew the original requirements.
- nayuki 4y agohttps://docs.oracle.com/javase/8/docs/api/java/nio/MappedByteBuffer.html https://docs.oracle.com/javase/8/docs/api/java/nio/MappedByt...
- ungamedplayer 4y agoWas the link to show me that mmap existed, or did that doc show some reason it could not be used.