Fun with java.net.URL

I'm writing a little app to check the health status of my server and came accross a confusing feature in java.net.URL. I remembered I struggle with this behaviour when I came from C# to Java. So this might be helpful for Java newbies.

URL url1 = new URL("http://pikodat.com");  
URL url2 = new URL("http://blog.pikodat.com");

System.out.println(url1.equals(url2));  

The output is:

true  

What? This URLs are not the same, they just run under the same IP. So this sound like a design flaw.

And here is the funny part: While disconnecting the internet connection the output is:

false  

That means that if you have an Internet connection there is a DNS lookup in the equals method. Imagine a map full of URLs...this might be horrible slow.

Let me clarify that this sounds like a bug, but in fact it's documented in the JavaDocs.

By the way, java.net.URI is here to rescue:

        URI uri1 = new URI("http://pikodat.com");
        URI uri2 = new URI("http://blog.pikodat.com");

        System.out.println(uri1.equals(uri2));