Java的隐藏功能

本文翻译自:Hidden Features of Java

看完C#的隐藏功能后,我想知道Java的一些隐藏功能是什么?


#1楼

参考:https://stackoom.com/question/41w/Java的隐藏功能


#2楼

Using this keyword for accessing fields/methods of containing class from an inner class. 使用关键字从内部类访问包含类的字段/方法。 In below, rather contrived example, we want to use sortAscending field of container class from the anonymous inner class. 在下面的示例中,我们想要使用匿名内部类中容器类的sortAscending字段。 Using ContainerClass.this.sortAscending instead of this.sortAscending does the trick. 使用ContainerClass.this.sortAscending而不是this.sortAscending可以解决问题。

import java.util.Comparator;

public class ContainerClass {
boolean sortAscending;
public Comparator createComparator(final boolean sortAscending){
    Comparator comparator = new Comparator<Integer>() {

        public int compare(Integer o1, Integer o2) {
            if (sortAscending || ContainerClass.this.sortAscending) {
                return o1 - o2;
            } else {
                return o2 - o1;
            }
        }

    };
    return comparator;
}
}

#3楼

This is not exactly "hidden features" and not very useful, but can be extremely interesting in some cases: 这并不是完全“隐藏的功能”,也不是很有用,但是在某些情况下可能会非常有趣:
Class sun.misc.Unsafe - will allow you to implement direct memory management in Java (you can even write self-modifying Java code with this if you try a lot): sun.misc.Unsafe类-将允许您在Java中实现直接内存管理 (如果尝试很多,甚至可以使用此代码编写自修改Java代码):

public class UnsafeUtil {

    public static Unsafe unsafe;
    private static long fieldOffset;
    private static UnsafeUtil instance = new UnsafeUtil();

    private Object obj;

    static {
        try {
            Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);

            unsafe = (Unsafe)f.get(null);
            fieldOffset = unsafe.objectFieldOffset(UnsafeUtil.class.getDeclaredField("obj"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
}

#4楼

You can define an anonymous subclass and directly call a method on it even if it implements no interfaces. 您可以定义一个匿名子类并直接在其上调用方法,即使该类未实现任何接口。

new Object() {
  void foo(String s) {
    System.out.println(s);
  }
}.foo("Hello");

#5楼

语言级别的assert关键字。


#6楼

I think another "overlooked" feature of java is the JVM itself. 我认为Java的另一个“被忽略”功能是JVM本身。 It is probably the best VM available. 它可能是最好的VM。 And it supports lots of interesting and useful languages (Jython, JRuby, Scala, Groovy). 它支持许多有趣且有用的语言(Jython,JRuby,Scala,Groovy)。 All those languages can easily and seamlessly cooperate. 所有这些语言都可以轻松无缝地协作。

If you design a new language (like in the scala-case) you immediately have all the existing libraries available and your language is therefore "useful" from the very beginning. 如果您设计一种新的语言(例如在scala案例中),您将立即拥有所有现有的库,因此您的语言从一开始就是“有用的”。

All those languages make use of the HotSpot optimizations. 所有这些语言都利用了HotSpot优化。 The VM is very well monitor and debuggable. VM非常易于监视和调试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值