Home » Java » What is the underscore actually doing in this Java code? [closed]

What is the underscore actually doing in this Java code? [closed]

Posted by: admin November 30, 2017 Leave a comment

Questions:

I just began to learn Java.

My friend who is helping me study just sent me this and said ‘figure this out’.

Unfortunately I am unable to read this. It looks like Perl to me.

class _{_ _;_(){_=this;}}

What does it mean?

Answers:

_ is the class name. It’s a very confusing one, but it works!

With the class renamed:

class Something {Something something;Something(){something=this;}}

And cleaned up:

class Something {
    Something something;
    Something() {
        something=this;
    }
}

And you can go crazy with this odd naming :)

class _{_ __;_ ____;_(){__=this;____=__;}_(_ ___){__=___;}}

In fact, Unicode is even supported, so this is valid:

class 合法類別名稱{合法類別名稱(){}}

Questions:
Answers:

_ is the class name, underscore is a valid Java variable name, you just need to indent your code to deobfuscate it:

class _{
    _ _;
    _(){
     _=this;
   }
}

Like:

class A{
    A A;
    A(){
     A=this;
   }
}

Edit: thanks to @Daniel Fischer

Type names and variable names have different namespaces. and for example code class FOO { FOO FOO; } is valid in Java.

Summary

  • _ is a class name e.g at class _{
  • _ is a class member name e.g at _ _; and _=this
  • _ is a constructor name e.g. at _()

Remember: Java uses six different namespaces:

  • Package names,
  • type names,
  • field (variable) names,
  • method names,
  • local variable names (including parameters), and
  • labels.

In addition, each declared enum has its own namespace. Identical names of different types do not conflict; for example, a method may be named the same as a local variable.

Questions:
Answers:

well that’s good example . Java allows unicode to be identifiers so you can write something like:

class ⲥlass {
ⲥlass claѕѕ;
}

here class name’s c is ‘ⲥ’ (U+2CA5 COPTIC SMALL LETTER SIMA) and

object name’s ‘ѕ’ (U+0455 CYRILLIC SMALL LETTER DZE).