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.
- 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 with
- hashCode() : 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 :
getAddress() : /127.0.0.1 getBroadcast() : /127.255.255.255 PrefixLength : 8 Hashcode : -16777208 toString() : /127.0.0.1/8 [/127.255.255.255] -------------------- 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- java.net.NetworkInterface class
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2