装配
List<Integer> numbers = new ArrayList<Integer>()
{{ add(1); add(2); }};
Map<String,String> codes = new HashMap<String,String>()
{{
put("1","one");
put("2","two");
}};
类路径通配符
java -classpath ./lib/* so.Main
协变量返回类型
class Souper {
Collection<String> values() {
...
}
}
class ThreadSafeSortedSub extends Souper {
@Override
ConcurrentSkipListSet<String> values() {
...
}
}
在finally块中进行控制转移会丢弃任何异常
不要在catch / finally块内引起异常,否则您也将丢弃原始异常
try块内执行System.exit(),则不会调用finally块
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;
}
}
instanceof以不需要检查null!的方式实现
if( aObject instanceof String )
{
...
}
枚举中允许方法和构造函数
enum Cats {
FELIX(2), SHEEBA(3), RUFUS(7);
private int mAge;
Cats(int age) {
mAge = age;
}
public int getAge() {
return mAge;
}
}
泛型方法的类型参数可以显式指定
Collections.<String,Integer>emptyMap()
可以使用枚举来实现接口
public interface Room {
public Room north();
public Room south();
public Room east();
public Room west();
}
public enum Rooms implements Room {
FIRST {
public Room north() {
return SECOND;
}
},
SECOND {
public Room south() {
return FIRST;
}
}
public Room north() { return null; }
public Room south() { return null; }
public Room east() { return null; }
public Room west() { return null; }
}
可变arity函数
public void foo(String... bars) {
for (String bar: bars)
System.out.println(bar);
}
创建快速摆架原型
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add( new JLabel("Hey there"){{
setBackground(Color.black);
setForeground( Color.white);
}});
panel.add( new JButton("Ok"){{
addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent ae ){
System.out.println("Button pushed");
}
});
}});
frame.add( panel );
JFrame frame = new JFrame(){{
add( new JPanel(){{
add( new JLabel("Hey there"){{
setBackground(Color.black);
setForeground( Color.white);
}});
add( new JButton("Ok"){{
addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent ae ){
System.out.println("Button pushed");
}
});
}});
}});
}};
最终的初始化可以推迟 最终变量的值必须设置一次 从Java 5开始,'final’也具有线程安全性,因此在构造函数期间设置final变量非常宝贵。
public Object getElementAt(int index) {
final Object element;
if (index == 0) {
element = "Result 1";
} else if (index == 1) {
element = "Result 2";
} else {
element = "Result 3";
}
return element;
}
定义一个匿名子类并直接在其上调用方法,即使该类未实现任何接口
new Object() {
void foo(String s) {
System.out.println(s);
}
}.foo("Hello");
asList方法java.util.Arrays允许将可变参数,通用方法和自动装箱完美结合
List<Integer> ints = Arrays.asList(1,2,3);
http
class Example
{
public static void main(String[] args)
{
System.out.println("Hello World!");
http://Phi.Lho.free.fr
System.exit(0);
}
}
类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);
}
};
}
每个类文件均以十六进制值0xCAFEBABE开头,以将其标识为有效的JVM字节码
DelayQueue的元素在指定的延迟后可用。
java.util.Timer和TimerTask可以安全地停下来。
在纳秒,微秒,毫秒和秒之间进行转换时,java.util.concurrent.TimeUnit非常有用。
可以从未声明抛出任何东西的方法中抛出检查异常。
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!"));
}
}
联网情况下自动解析
new URL("http://www.yahoo.com").equals(new URL("http://209.191.93.52"))
//true
静态导入可“增强”该语言
List<String> ls = List("a", "b", "c");
List<Map<String, String>> data = List(Map( o("name", "michael"), o("sex", "male")));
不使用main方法
public class WithoutMain {
static {
System.out.println("Look ma, no main!!");
System.exit(0);
}
}
$ java WithoutMain
Look ma, no main!!
自绑
class SelfBounded<T extends SelfBounded<T>> {
}
subList(int fromIndex,int toIndex)消除了对显式范围操作(数组通常存在的那种范围)的需要。任何期望列表的操作都可以通过传递subList视图而不是整个列表来用作范围操作。例如删除一系列元素
list.subList(from, to).clear();
StackOverflow相关问题
https://stackoverflow.com/questions/15496/hidden-features-of-java