Skip to Content
Java Performance, 2nd Edition
book

Java Performance, 2nd Edition

by Scott Oaks
February 2020
Intermediate to advanced
448 pages
13h 15m
English
O'Reilly Media, Inc.
Book available
Content preview from Java Performance, 2nd Edition

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

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
Java in a Nutshell, 8th Edition

Java in a Nutshell, 8th Edition

Benjamin J. Evans, Jason Clark, David Flanagan

Publisher Resources

ISBN: 9781492056102Errata PageSupplemental Content