Hidden features of Java


Below are some hidden features of java.

Covariant return types
It is pretty poorly publicised, as it is an unsexy addition, but as I understand it, is absolutely necessary for generics to work.

Essentially, the compiler now allows a subclass to narrow the return type of an overridden method to be a subclass of the original method's return type. So this is allowed:

class Souper {
Collection values() {

class ThreadSafeSortedSub extends Souper {
ConcurrentSkipListSet values() {

You can call the subclass's values method and obtain a sorted thread safe Set of Strings without having to down cast to the ConcurrentSkipListSet.

Labeled blocks

for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
for (int k = j; k < N; ++k) {
//do something here
break getmeout;

Allowing methods and constructors in enums

enum Cats {

private int mAge;
Cats(int age) {
mAge = age;
public int getAge() {
return mAge;

You can even have a "constant specific class body" which allows a specific enum value to override methods.
a label with a comment

Not really a feature, but an amusing trick

class Example
public static void main(String[] args)
System.out.println("Hello World!");


Cool Equals

new URL("http://www.yahoo.com").equals(new URL(""))

Load a resource (property file, xml, xslt, image etc) from deployment jar file

this.getClass().getClassLoader().getResourceAsStream( ... ) ;

There are plenty more that i might not be aware of . IF you know any one/more of them then please contribute via comments.


Paulo Faria said...

I hope you like DNS connections if you're going to use that equals method. One of the reasons you shouldn't use URL in a map.

Also there are some hilariously bitter C# apologists bemoaning the lack of "using" in java, when "using" is just a method definition away, and more flexible to boot. If this was put in the java.lang, and the compiler implicitly imported static methods from there, the lazy bastards might shut up about using.

* Close closeables. Use this in a finally clause.
public static void close(Closeable... closeables) {
for (Closeable c : closeables) {
if (c != null) {
try {
} catch (Exception ex) {
Logger.getLogger(IoUtils.class.getName()).log(Level.WARNING, "Couldnt close Closeable", ex);

import static IoUtils.*;

void whatever(){
Resource wtfClosureFetishists = null;


Geek Head said...

Dear Reader,

There are my more hidden trick you can do in java that I might not aware of. If you know any please share some via comment.

Munsey said...

I would not ever recommend anyone use java.net.URL. First, it requires an internet connection, because it does a DNS lookup, which means it is slooooow. Don't EVER try to put URLs into a Set, because a Set checks for duplicates using the equals method. It can take minutes per insert even with a fairly small Set.
Second, URL.equals can return false when it should intuitively return true, and true when it should return false. For example, if www.mycompany.com uses DNS load balancing, two successive calls to DNS will return two different IP addresses, causing new URL("http://www.mycompany.com").equals(new URL("http://www.mycompany.com")) to return false. On the other hand, for virtual domains that share the same IP address, equals would return true, although intuitively I would argue that it shouldn't.

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment