Chapter 12. Java SE API Tips

This chapter covers areas of the Java SE API that have implementation quirks affecting their performance. Many such implementation details exist throughout the JDK; these are the areas where I consistently uncover performance issues (even in my own code). This chapter includes details on the best way to handle strings (and especially duplicate strings); ways to properly buffer I/O; classloading and ways to improve startup of applications that use a lot of classes; proper use of collections; and JDK 8 features like lambdas and streams.


Strings are (unsurprisingly) the most common Java object. In this section, we’ll look at a variety of ways to handle all the memory consumed by string objects; these techniques can often significantly reduce the amount of heap your program requires. We’ll also cover a new JDK 11 feature of strings involving concatenation.

Compact Strings

In Java 8, all strings are encoded as arrays of 16-bit characters, regardless of the encoding of the string. This is wasteful: most Western locales can encode strings into 8-bit byte arrays, and even in a locale that requires 16 bits for all characters, strings like program constants often can be encoded as 8-bit bytes.

In Java 11, strings are encoded as arrays of 8-bit bytes unless they explicitly need 16-bit characters; these strings are known as compact strings. A similar (experimental) feature in Java 6 was known as compressed strings; compact strings are conceptually the ...

Get Java Performance, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.