2024-04-20 09:00:01

Java的隐藏特性

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


当前回答

实际上,我喜欢Java的原因是它很少有隐藏的技巧。这是一种非常明显的语言。以至于15年过去了,我能想到的几乎每一个都已经列在这几页纸上了。

也许大多数人都知道Collections.synchronizedList()将同步添加到列表中。除非阅读文档,否则您不可能知道的是,您可以通过同步列表对象本身来安全地迭代该列表的元素。

有些人可能不知道CopyOnWriteArrayList,而Future表示一种抽象多线程结果访问的有趣方法。

您可以通过各种管理、代理和附加api连接到虚拟机(本地或远程),获取关于GC活动、内存使用、文件描述符甚至对象大小的信息。

虽然TimeUnit可能比long更好,但我更喜欢Wicket的Duration类。

其他回答

这是我的清单。

我最喜欢的(也是最可怕的)隐藏特性是,您可以从没有声明抛出任何东西的方法中抛出检查异常。

import java.rmi.RemoteException;

class Thrower {
    public static void spit(final Throwable exception) {
        class EvilThrower<T extends Throwable> {
            @SuppressWarnings("unchecked")
            private void sneakyThrow(Throwable exception) throws T {
                throw (T) exception;
            }
        }
        new EvilThrower<RuntimeException>().sneakyThrow(exception);
    }
}

public class ThrowerSample {
    public static void main( String[] args ) {
        Thrower.spit(new RemoteException("go unchecked!"));
    }
}

此外,你可能想知道你可以抛出'null'…

public static void main(String[] args) {
     throw null;
}

猜猜这打印了什么:

Long value = new Long(0);
System.out.println(value.equals(0));

猜猜这返回什么:

public int returnSomething() {
    try {
        throw new RuntimeException("foo!");
    } finally {
        return 0;
    }
}

优秀的开发人员不应该对上述情况感到惊讶。


在Java中,你可以用以下几种有效的方式来声明数组:

String[] strings = new String[] { "foo", "bar" };
// the above is equivalent to the following:
String[] strings = { "foo", "bar" };

所以下面的Java代码是完全有效的:

public class Foo {
    public void doSomething(String[] arg) {}

    public void example() {
        String[] strings = { "foo", "bar" };
        doSomething(strings);
    }
}

是否有任何有效的理由说明下面的代码不应该是有效的?

public class Foo {

    public void doSomething(String[] arg) {}

    public void example() {
        doSomething({ "foo", "bar" });
    }
}

我认为,上述语法可以有效地替代Java 5中引入的可变参数。并且,与之前允许的数组声明更加一致。

我认为java的另一个“被忽视”的特性是JVM本身。它可能是可用的最好的VM。它支持许多有趣和有用的语言(Jython, JRuby, Scala, Groovy)。所有这些语言都可以轻松无缝地合作。

如果你设计了一种新的语言(就像在scala的例子中),你会立刻拥有所有现有的库,因此你的语言从一开始就是“有用的”。

所有这些语言都利用了HotSpot优化。VM可以很好地监控和调试。

如果不使用默认初始化式,Java处理在变量定义上做了一个巧妙的技巧。

{
   int x;

   if(whatever)
      x=1;

   if(x == 1)
      ...
}

这将在编译时给您一个错误,即您有一个X未正确定义的路径。这帮助了我几次,我已经开始考虑默认的初始化,像这样:

int x=0;
String s=null;

这是一个不好的模式,因为它阻碍了这种有用的检查。

也就是说,有时很难解决这个问题——当它作为默认值有意义时,我不得不回去编辑=null,但我再也没有在第一次传递时把它放在里面了。

我知道Java 6包括脚本支持,但我最近才发现jrunscript, 它可以交互式地解释和运行JavaScript(以及其他脚本语言,如Groovy),有点像Ruby中的Python shell或irb

前几天我对实例初始化器感到惊讶。我删除了一些代码折叠方法,最终创建了多个实例初始化器:

public class App {
    public App(String name) { System.out.println(name + "'s constructor called"); }

    static { System.out.println("static initializer called"); }

    { System.out.println("instance initializer called"); }

    static { System.out.println("static initializer2 called"); }

    { System.out.println("instance initializer2 called"); }

    public static void main( String[] args ) {
        new App("one");
        new App("two");
  }
}

执行main方法将显示:

static initializer called
static initializer2 called
instance initializer called
instance initializer2 called
one's constructor called
instance initializer called
instance initializer2 called
two's constructor called

我想,如果您有多个构造函数并且需要通用代码,那么这些将很有用

它们还提供了初始化类的语法糖:

List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};

Map<String,String> codes = new HashMap<String,String>(){{ 
  put("1","one"); 
  put("2","two");
}};