What is the underscore actually doing in this Java code?

JavaSyntax

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

_ 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 合法類別名稱{合法類別名稱(){}}

Solution 2 - Java

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

Solution 3 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionanother ordinaryView Question on Stackoverflow
Solution 1 - JavatckmnView Answer on Stackoverflow
Solution 2 - JavaGrijesh ChauhanView Answer on Stackoverflow
Solution 3 - JavaashgkwdView Answer on Stackoverflow