- Classpath wild cards since Java 6: java -classpath ./lib/* so.Main Instead of java -classpath ./lib/log4j.jar:./lib/commons-codec.jar:./lib/commons-httpclient.jar:./lib/commons-collections.jar:./lib/myApp.
- JDK 1.6_07+ contains an app called VisualVM (bin/jvisualvm.exe)
- variable arity. So, instead of just passing an array, now you can do the following
public void foo(String... bars) { for (String bar: bars) System.out.println(bar); }
bars is automatically converted to array of the specified type
- javap disassembler which comes with Sun's JDK is not widely known or used.
- // For each Object, instantiated as foo, in myCollection
System.out.println(foo.toString());
}
- checking for null is not necessary.
Instead of:
if( null != aObject && aObject instanceof String )
{
...
}
just use:
if( aObject instanceof String )
{
...
}
- anonymous subclass or anonymous impl of an interface:
//anonymous subclass and directly call a method on it even if it implements no interfaces.
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(2000);
System.out.println("sleeping");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void initImplementationOfInterface(){
new TestInterface(){
@Override
public String printId() {
// TODO Auto-generated method stub
String someString = "time "+System.currentTimeMillis();
System.out.println(someString);
return someString;
}
}.printId();
}
Source attributation: http://stackoverflow.com/questions/15496/hidden-features-of-java