Skip to content

gebes/HackJava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

HackJava

This code manipulates Java via reflect. It should print 4, but it doesnt. Useful to confuse your programming teacher.

import java.lang.reflect.Field;

public class Main {

    public static void main(String[] args) throws Exception {
        hackJava();
        System.out.printf("%d", 2 + 2);
    }
    
    public static void hackJava()throws Exception{
        Class cache = Integer.class.getDeclaredClasses()[0];
        Field c = cache.getDeclaredField("cache");
        c.setAccessible(true);
        Integer[] array = (Integer[]) c.get(cache);
        array[132] = 5;
    }
}

Output:

5

Releases

No releases published

Packages

No packages published

Languages