Re: InetAddress.equals() Anomoly?

From: Bruno Jouhier [MVP] (bjouhier_at_club-internet.fr)
Date: 01/08/05


Date: Sat, 8 Jan 2005 09:31:14 +0100

I don't agree.

Two objects are equal if they can be used interchangeably and if they
produce the same results when any arbitrary method is applied to them.

In your case:
    dnsaddr.toString() produces firenet.us/12.34.75.101
    ipaddr.toString() produces grande.rivcom.net/12.34.75.101

So, the objects are not equal because they don't produce the same result
when you apply toString() to them.

If you consider that two InetAddress should be equal as soon as they have
the same IP, you take a biased view, you consider that your equals method
should take the IP part into account and completely ignore the name side of
the INetAddress object. This is wrong. If you start overriding equals to
give it "partially equals" semantics, you are going to run into some
trouble.

So, this is not a bug in the JDK. If you want to use the IP as a hash key,
you should use the IP (formatted as a string, or as an integer) as key, you
should not use InetAddress because this is a richer object that carries more
information than an IP.

Bruno.

"Pete Loveall" <psl@ametx.com.NO_SPAM> a écrit dans le message de news:
ePrGKCN9EHA.1296@TK2MSFTNGP10.phx.gbl...
> Definite J# bug!
>
> The following output was produced by the code below:
>
> 203574117 203574117
> firenet.us/12.34.75.101=grande.rivcom.net/12.34.75.101->false
> grande.rivcom.net/12.34.75.101=firenet.us/12.34.75.101->false
>
> import java.net.*;
>
> public class Class1
> {
> public Class1()
> {
> }
> /** @attribute System.STAThread() */
> public static void main(String[] args)
> {
> InetAddress dnsaddr = null;
> InetAddress ipaddr = null;
> try
> {
> dnsaddr = InetAddress.getByName("firenet.us");
> ipaddr = InetAddress.getByName("12.34.75.101");
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> System.out.println(dnsaddr.hashCode()+" "+ipaddr.hashCode());
> System.out.println(dnsaddr.toString()+"="+ipaddr.toString()+"->"+dnsaddr.equals(ipaddr));
> System.out.println(ipaddr.toString()+"="+dnsaddr.toString()+"->"+ipaddr.equals(dnsaddr));
> try
> {
> System.in.read();
> }
> catch (Exception e){}
> }
> }
>
>



Relevant Pages

  • Re: Improving String.equals() implementation
    ... I don't understand your objection. ... The second method should catch the exception and return 'false'. ... If one is considering an optimization for a project, one cannot consider "optimizations" that break contract behavior. ... It is true that one of the equals() methods in my code ...
    (comp.lang.java.programmer)
  • Re: InetAddress.equals() Anomoly?
    ... Compares this object against the specified object. ... array components is the same for the byte arrays. ... > If you consider that two InetAddress should be equal as soon as they have ... If you start overriding equals ...
    (microsoft.public.dotnet.vjsharp)
  • Re: InetAddress.equals() Anomoly?
    ... > Compares this object against the specified object. ... The J# implementation is buggy because it does not follow the specs. ... The specs are bad because they violate the normal semantics of "equals". ... >> If you consider that two InetAddress should be equal as soon as they have ...
    (microsoft.public.dotnet.vjsharp)
  • Re: Performance issue (bug?) with .NET 2 ListView
    ... Ok, I see what you are saying, yes, it will delegate to Equals, but ... the first check in the Equals overload is against the length. ... Perhaps an exception is being generated and swallowed. ... private int ConvertToInt ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Equals
    ... Theorically equals should be a symetrical operation (docs state among ... >> However if either the objects are null as an exception will be thrown ... vtable for null instances of Foo: ... Bar = Object.ThrowNullException ...
    (microsoft.public.dotnet.languages.csharp)