Re: InetAddress.equals() Anomoly?
From: Bruno Jouhier [MVP] (bjouhier_at_club-internet.fr)
Date: 01/08/05
- Next message: Lars-Inge Tønnessen [VJ# MVP]: "Re: J# support for java 1.3?"
- Previous message: Manuel J. Goyenechea: "Re: J# support for java 1.3?"
- In reply to: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Next in thread: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Reply: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Messages sorted by: [ date ] [ thread ]
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){}
> }
> }
>
>
- Next message: Lars-Inge Tønnessen [VJ# MVP]: "Re: J# support for java 1.3?"
- Previous message: Manuel J. Goyenechea: "Re: J# support for java 1.3?"
- In reply to: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Next in thread: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Reply: Pete Loveall: "Re: InetAddress.equals() Anomoly?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|