1. Home
  2. Docs
  3. Network Programming
  4. Internet Address
  5. Object Methods

Object Methods

Introduction

Like every other class, java.net.InetAddress inherits from java.lang.Object. Thus, it has access to all the methods of that class. It overrides three methods to provide more specialized behavior:

public boolean equals(Object o)
public int hashCode()
public String toString()

An object is equal to an InetAddress object only if it is itself an instance of the InetAddress class and it has the same IP address. It does not need to have the same hostname. Thus, an InetAddress object for www.ibiblio.org is equal to an InetAddress object for www.cafeaulait.org because both names refer to the same IP address.

public boolean equals(Object o)

An object is equal to an InetAddress object only if it is itself an instance of the InetAddress class and it has the same IP address. It does not need to have the same hostname. Thus, an InetAddress object for www.ibiblio.org is equal to an InetAddress object for www.cafeaulait.org because both names refer to the same IP address. This method can be overridden in any classes like this.

@Override
public boolean equals(Object o) {
    Overrides obj = (Overrides) o;
    return obj.x == this.x;
}

public int hashCode()

The int that hash
Code() returns is calculated solely from the IP address. It does not take the hostname into account. If two InetAddress objects have the same address, then they have the same hash code, even if their hostnames are different. This method can be overridden in any classes like this.

@Override
public int hashCode() {
    return x.hashCode();
}

public String toString()

This method when passing InetAddress objects to System.out.println(). When we run the program we can see the string produced by toString() equivalent. This method can be overridden in any classes like this.

@Override
public String toString() {
    return x + " from abc class";
}
Was this article helpful to you? Yes No

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *