Java.net.InterfaceAddress class in Java Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 :getAddress() : Returns an InetAddress for this address.Syntax : public InetAddress getAddress()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()getNetworkPrefixLength() : Returns the prefix length for this interface address, i.e. subnet mask for this address.Syntax :public short getNetworkPrefixLength()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 withhashCode() : Returns the hashcode for this interface address. Syntax :public int hashCode()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 // Java program to illustrate methods of // Java.net.InterfaceAddress class import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.SocketException; 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()); System.out.println("--------------------\n"); } } } Output :getAddress() : /127.0.0.1getBroadcast() : /127.255.255.255PrefixLength : 8Hashcode : -16777208toString() : /127.0.0.1/8 [/127.255.255.255]--------------------getAddress() : /0:0:0:0:0:0:0:1getBroadcast() : nullPrefixLength : 128Hashcode : 129toString() : /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- java.net.NetworkInterface classReferences :Official Java Documentation Comment More infoAdvertise with us Next Article Java.net.InterfaceAddress class in Java R Rishabh Mahrsee Improve Article Tags : Java Practice Tags : Java Similar Reads java.net.InetAddress Class in Java public class InetAddress extends Object implements Serializable: The java.net.InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses. There are 2 types of address 6 min read java.net.FileNameMap Interface in Java java.net.FileNameMap is a Java Interface. FileNameMap interface is a part of the java.net package. FileNameMap provides a mechanism to map between a file name and a MIME type string. We can use FileNameMap getContentTypeFor method to get the MIME type of the specified file. Syntax: public interface 2 min read Java Class vs Interfaces In Java, the difference between a class and an interface is syntactically similar; both contain methods and variables, but they are different in many aspects. The main difference is, A class defines the state of behaviour of objects.An interface defines the methods that a class must implement.Class 5 min read java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part 4 min read Java.net.JarURLConnection class in Java Prerequisite - JAR files in Java What is a Jar file? JavaArchive(JAR) bundles all the classes in one package. Since the archive is compressed and can be downloaded in a single HTTP connection, it is often faster to download the archive than to download individual classes. Although jar bundles all th 4 min read Nested Interface in Java In Java, we can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declar 5 min read java.net.Proxy Class in Java A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are 3 min read Java.net.DatagramSocket class in Java Datagram socket is a type of network socket which provides connection-less point for sending and receiving packets. Every packet sent from a datagram socket is individually routed and delivered. It can also be used for sending and receiving broadcast messages. Datagram Sockets is the java's mechanis 9 min read Java.net.DatagramPacket class in Java This class provides facility for connection less transfer of messages from one system to another. Each message is routed only on the basis of information contained within the packet and it may be possible for different packets to route differently. There is also no guarantee as to whether the messag 5 min read Interface Naming Conflicts in Java Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction. Naming Conflicts occur when a class i 5 min read Like