Is it possible to implement a more compact and optimal data type than String in Java?


The String data type (i.e. public final class String ) in Java 1.8 takes up 3168 lines of source code (including all deprecated methods). Often, no one uses all the features of this type in a particular project.

For the sake of sports interest, I want to implement a compact data type a la public final class CompactFastString with a minimal set of methods and properties.

The goal is more compact in terms of memory used and faster to run. The goal is more laboratory than real.

But there is an opinion that it is difficult to implement a more optimal solution than the standard Java SE library.

What do you think about this?


Faster in the general case will not work. The standard types of the java.lang are highly optimized at the JVM level. The source code that you see in these classes is for information and debugging, but in reality it is replaced with extremely fast instructions, taking into account the capabilities of the processor. For example, string comparison will not be character-by-character, but using batch SSE instructions that can compare 4 bytes per processor cycle. This is the advantage of JIT compilation.

PS The architects of the virtual machine are also not very happy with the occupied memory for strings, so it is planned to make 2 modes of string operation in future versions – utf-16 and only latin-1. But again, this will be at the JVM level.

Scroll to Top