Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles class in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

This class represents a network interface address. Every device that has an IP address has an IP address on the network interface. In fact the ping command doesn’t ping a device but the devices interface address. Java provides certain methods to deal with interface addresses which can be used in places where there is a need to know the topology of the network, for fault detection in a network etc.
Methods : 

  1. getAddress() : Returns an InetAddress for this address. 
Syntax : public InetAddress getAddress()
  1. getBroadcast() : Returns the InetAddress for the broadcast address for this interface address. As only IPv4 addresses have broadcast addresses, null would be returned on using an IPv6 address. 
Syntax :public InetAddress getBroadcast()
  1. getNetworkPrefixLength() : Returns the prefix length for this interface address, i.e. subnet mask for this address. 
Syntax :public short getNetworkPrefixLength()
  1. equals() : Used for comparison of the specified object with this interface address. Returns true only if the given object is not null and represents same interface address as this object. 
Syntax :public boolean equals(Object obj)
Parameters :
obj : obj to compare with
  1. hashCode() : Returns the hashcode for this interface address. 
Syntax :public int hashCode()
  1. toString() : Returns a string representation of this interface address. The string is of the form : Interface Address/ prefix length. 
Syntax :public String toString()

Java Implementation : 


// Java program to illustrate methods of
// class
import java.util.List;
public class interfaceaddress
    public static void main(String[] args) throws SocketException
        // Modify according to your system
        NetworkInterface nif = NetworkInterface.getByIndex(1);
        List<InterfaceAddress> list = nif.getInterfaceAddresses();
        for (InterfaceAddress iaddr : list)
            // getAddress() method
            System.out.println("getAddress() : " + iaddr.getAddress());
            // getBroadcast() method
            System.out.println("getBroadcast() : " + iaddr.getBroadcast());
            // getNetworkPrefixLength() method
            System.out.println("PrefixLength : " + iaddr.getNetworkPrefixLength());
            // hashCode() method
            System.out.println("Hashcode : " + iaddr.hashCode());
            // toString() method
            System.out.println("toString() : " + iaddr.toString());

Output : 

getAddress() : /
getBroadcast() : /
PrefixLength : 8
Hashcode : -16777208
toString() : / [/]

getAddress() : /0:0:0:0:0:0:0:1
getBroadcast() : null
PrefixLength : 128
Hashcode : 129
toString() : /0:0:0:0:0:0:0:1/128 [null]

Please modify the index in getbyIndex() method in the above program as the network interface at index 1 might be null in your system. For more details about the this method, refer to- class 
References : 
Official Java Documentation 
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Last Updated : 08 Oct, 2021
Like Article
Save Article
Similar Reads
Related Tutorials