Java.net.Inet4Address class in Java

This class extends InetAddress class and represents an IPv4 address. It provides methods to interpret and display useful information about ip addresses.Methods of this class takes input in 4 formats:

d.d.d.d : When this format is used as input, each of the given values are assigned to 4 bytes of the IP address from left to right.

d.d.d : When this format is used as input, the last part is interpreted as 16 bit number and assigned to the rightmost 2 bytes as the host address. This is generally used for specifying class-B address.

d.d : When this format is used as input, the last part is interpreted as 24 bit number and assigned to the rightmost 3 bytes as the host address. This is generally used for specifying class-A address.

d : When this format is used as input, the given value is directly stored as network address without any rearrangement.

Methods :

getAddress() : returns raw IP address of this InetAddress object as an array. The order in which bytes appear in array are same as in IP address i.e. getAddress[0] will contain highest order byte.

Syntax : public byte[] getAddress()

getHostAddress() : returns IP address in textual form.

Syntax :public String getHostAddress()

isAnyLocalAddress() : returns true if this address represents a local address.

Syntax :public boolean isAnyLocalAddress()

isLinkLocalAddress() : returns true if this address is a link local address.

Syntax :public boolean isLinkLocalAddress()

isLoopbackAddress() : returns true if this address is a loopback address.

References :Java Official 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 contribute@geeksforgeeks.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.