DEV Community

Dhanushka madushan
Dhanushka madushan

Posted on

A Weird Java Code and How It Works

I discovered following strange Java code that print Just another Java hacker on your terminal.

Obviously, that piece of code looks like mess. Even if you copy this code into your favorite IDE, IDE could not figure out the syntax of this code. Trust me, this piece of code works, and this is how it works.

Obviously it has class name with main method. Let's clear out the code and format it. Now it look like this.

class Sig{
    public static void main(String...args){
        \u0066or(int\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
        )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
        +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));
    }
}

Now you can see that there are some unicode characters in this code. Next we'll extract all unicode characters. Also I'm removing /*goto/*$/%\u0126//^\u002A\u002F and \u002Array.const(~1)\*\u002F section since those code sections are just commented line written in unicode characters.

class Sig{
    public static void main(String...args){
        for(int $:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".toCharArray())
            System.out.print((char)(($>>+(~'"'&'#'))+('<'>>('\\'/'.'))));
    }
}

If you replace (char)(($>>+(~'"'&'#'))+('<'>>('\\'/'.'))) expression with relevant ascii values then it would be something like (char)(($>>+(~42&35))+(60>>(92/46))). This is equivalent to (char)(c / 2 + 15). Final output of the simplified code would be something like below.

class Sig{
    public static void main(String...args){
        for(int $:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".toCharArray())
            System.out.print((char)($ / 2 + 15));
    }
}

Now the mystery has been resolved. For loop take each of the characters from the string change it value according to the equation and print it.

Why don't you give it a try to write such a code with your favaourite language and comment it down?

Warning: Don't ever write this type of code in production repositories.

Top comments (2)

Collapse
 
fultonbrowne profile image
Fulton Browne

I ran it, it worked and intellij thinks its some kinda bug!

Collapse
 
dhanushkadev profile image
Dhanushka madushan

It's not intelligent enough to read unicode.