2024-04-20 09:00:01

Java的隐藏特性

在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?


当前回答

我个人很晚才发现java.lang.Void——结合泛型提高了代码的可读性,例如Callable<Void>

其他回答

标识符可以包含外文字符,比如变音:

而不是写:

String title="";

有人可以这样写:

String Überschrift="";

finally块中的控制权转移会丢弃任何异常。下面的代码不会抛出RuntimeException——它丢失了。

public static void doSomething() {
    try {
      //Normally you would have code that doesn't explicitly appear 
      //to throw exceptions so it would be harder to see the problem.
      throw new RuntimeException();
    } finally {
      return;
    }
  }

从http://jamesjava.blogspot.com/2006/03/dont-return-in-finally-clause.html

没读到过

Integer a = 1;
Integer b = 1;
Integer c = new Integer(1);
Integer d = new Integer(1);

Integer e = 128;
Integer f = 128;

assertTrue (a == b);   // again: this is true!
assertFalse(e == f); // again: this is false!
assertFalse(c == d);   // again: this is false!

通过搜索java的整数池(内部“缓存”从-128到127用于自动装箱)或查看integer . valueof来了解更多信息

您可以在枚举类的方法定义中切换(此)。当我发现这个方法真的有效时,我大声喊了一声“什么!”

这并不是一个真正的隐藏功能,但当我看到这个编译好的时候,它确实给了我一个很大的惊喜:

public int aMethod(){
    http://www.google.com
    return 1;
}

它被编译的原因是编译器将http://www.google.com行“http:”部分视为标签,而该行的其余部分是注释。

所以,如果你想写一些奇怪的代码(或模糊的代码),只要在那里放很多http地址。: -)