Master Java Obfuscation: 5 Crazy Tricks to Write Unreadable Code
This article reveals five advanced Java tricks—using Unicode escapes in comments, over‑complicating simple logic with bitwise shifts, tampering with Boolean.TRUE via reflection, forcing both branches of an if‑else to run, and leveraging the Unsafe class for low‑level memory manipulation—to deliberately make code hard to understand.
1. Hide Code in Comments
By inserting a Unicode escape sequence
\u000dinside a comment, the Java compiler treats it as a line break, turning the commented line into executable code. The example prints
coder Hydraeven though the statement appears inside a comment.
<code>public static void main(String[] args) {
// \u000d System.out.println("coder Hydra");
}</code>After compilation the comment becomes a real statement, so the program prints the hidden string.
2. Over‑Complicate Simple Logic
Instead of a straightforward sign check, the code uses the unsigned right‑shift operator
>>>to examine the sign bit of an
int. This makes the logic look obscure while achieving the same result.
<code>public void judge2(int x) {
if (x>>>31 == 0) {
// ...
} else if (x>>>31 == 1) {
// ...
}
}</code>3. Flip Boolean.TRUE via Reflection
By accessing the private static field
TRUEof
java.lang.Booleanthrough reflection, removing its
finalmodifier, and setting its value to
false, the program can make
Boolean.TRUEevaluate to
false, causing a seemingly correct program to print
false.
<code>static {
try {
Field trueField = Boolean.class.getDeclaredField("TRUE");
trueField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(trueField, trueField.getModifiers() & ~Modifier.FINAL);
trueField.set(null, false);
} catch (Exception e) {
e.printStackTrace();
}
}</code>4. Force Both Branches of an If‑Else
By crafting a condition that triggers a side‑effect—instantiating an anonymous inner class whose initializer calls the same method—the code can cause both the
ifand
elseblocks to execute during a single method call.
<code>public static void judge(String param) {
if (param == null || new IfTest(){{ IfTest.check(null); }}.equals("Hydra")) {
System.out.println("step one");
} else {
System.out.println("step two");
}
}</code>Running this prints both "step one" and "step two".
5. Low‑Level Memory Tricks with Unsafe
The
sun.misc.Unsafeclass allows direct memory allocation and manipulation. By allocating memory, writing an
int, and reading it back, the code demonstrates how to perform operations that bypass normal Java safety checks.
<code>void test() {
long addr = unsafe.allocateMemory(4);
unsafe.putInt(addr, 1);
int a = unsafe.getInt(addr);
System.out.println(a);
unsafe.freeMemory(addr);
}</code>Additional examples show setting memory byte‑by‑byte, copying memory blocks, and reading long values, illustrating the power—and danger—of the Unsafe API.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.