Understanding sun.misc.Unsafe

Shrouded in mystery, some say it's time for Java's Unsafe API to go -- or go public

Last week news broke that some developers are up in arms, claiming that Oracle's proposed removal of sun.misc.Unsafe in Java 9 will wreck the Java ecosystem. For many developers not embroiled in low-level programming, however, the private API is likely shrouded in mystery. What is this API so dangerous that its very name is unsafe?

Blogger and open source contributor Rafael Winterhalter explains in "Understanding sun.misc.Unsafe" that unsafe code is often required for low-level programming, where developers modify platform functionality for a specific purpose. While JNI (Java Native Interface) is considered the safest option for low-level Java programming, many open source projects have used Unsafe as a less constrained workaround.

Winterhalter presents one of several examples utilizing Unsafe to overcome a Java programming hurdle:

The first time I made use of the Unsafe class was for creating an instance of a class without calling any of the class's constructors. I needed to proxy an entire class which only had a rather noisy constructor but I only wanted to delegate all method invocations to a real instance [...] Creating a subclass was easy and if the class had been represented by an interface, creating a proxy would have been a straight-forward task. With the expensive constructor, I was however stuck. By using the Unsafe class, I was however able to work my way around it.

See "Understanding sun.misc.Unsafe" and "Java magic, Part 4: sun.misc.Unsafe" for additional examples that demonstrate the why and how of using Unsafe in Java programs.

Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.