InterfaceAddress equals(Object) in Java

equals(Object): This method is available in the java.net.InterfaceAddress class of Java.

Syntax:

boolean java.net.InterfaceAddress.equals(Object obj)

This method takes one argument. This method compares this object against the specified object. The result is true if and only if the argument is not null and it represents the same interface address as this object.

Two instances of InterfaceAddress represent the same address if the InetAddress, the prefix length and the broadcast are the same for both.

Parameters: One parameter is required for this method.

obj: the object to compare against.

Returns: true if the objects are the same; false otherwise.

Exceptions: NA

Approach

Java

package com.InterfaceAddress;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.List;

public class InterfaceAddressequals {
    public static void main(String[] args) throws SocketException {

        NetworkInterface nif = NetworkInterface.getByIndex(1);
        List<InterfaceAddress> list = nif.getInterfaceAddresses();

        InterfaceAddress interfaceAddress = list.get(0);

        System.out.println(interfaceAddress.equals(interfaceAddress));
    }
}

Output:

true


Some other methods of InterfaceAddress

equals(Object)This method compares this object against the specified object. 

getAddress()This method returns an InetAddress for this address.

getBroadcast()This method returns an InetAddress for the broadcast address for this InterfaceAddress.

getNetworkPrefixLength()This method returns the network prefix length for this address.

hashCode()This method returns a hashcode for this Interface address.

toString()This method converts this Interface address to a String. The string returned is of the form: InetAddress / prefix-length [ broadcast address ].

No comments:

Post a Comment