Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

syntax

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?

like image 825
another ordinary Avatar asked Mar 26 '13 22:03

another ordinary


People also ask

What does the underscore do in Java?

In Java SE 7 and later, any number of underscore characters ( _ ) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

Can I use underscore in Java variable?

In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.


2 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 合法類別名稱{合法類別名稱(){}} 
like image 52
tckmn Avatar answered Sep 19 '22 08:09

tckmn


_ 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.

like image 40
Grijesh Chauhan Avatar answered Sep 21 '22 08:09

Grijesh Chauhan