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.
Syntax :public boolean isLoopbackAddress()
- isMCGlobal() : returns true if this multicast address has global scope.
Syntax :public boolean isMCGloabal()
- isMCLinkLocal() : returns true if this multicast address has link scope.
Syntax :public boolean isMCLinkLocal()
- isMCNodeLocal() : returns true if this multicast address has node scope.
Syntax :public boolean isMCNodeLocal()
- isMCOrgLocal() : returns true if this multicast address has organisation scope.
Syntax :public boolean isMCOrgLoacal()
- isMCSiteLocal() : returns true if this multicast address has site scope.
Syntax :public boolean isMCSiteLocal()
- isMulticastAddress() : returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits.
Syntax :public boolean isMulticastAddress()
- isSiteLocalAddress(): returns true if this address is a site local address.
Syntax :public boolean isSiteLocalAddress()()
- hashCode() : returns the hashcode associated with this address object.
Syntax : public int hashCode()
- equals() : returns true if this ip address is same as that of the object specified. Equals() method don’t consider host names while comparing and only consider IP address associated.
Syntax : public boolean equals(Object obj) Parameters : obj : object to compare with
Java Implementation :
// Java program to illustrate various // Inet4Address class methods import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; public class inet4add { public static void main(String args[]) throws UnknownHostException { String url = "www.geeksforgeeks.org"; Inet4Address ip1 = (Inet4Address) Inet4Address.getByName(url); Inet4Address ip2 = (Inet4Address) InetAddress.getByName("www.yahoo.com"); // Following methods checks the property of the thus created object. // getAddress() method System.out.println("Address : " + Arrays.toString(ip1.getAddress())); // getHostAddress() method System.out.println("Host Address : " + ip1.getHostAddress()); // isAnyLocalAddress() method System.out.println("isAnyLocalAddress : " + ip1.isAnyLocalAddress()); // isLinkLocalAddress() method System.out.println("isLinkLocalAddress : " + ip1.isLinkLocalAddress()); // isLoopbackAddress() method System.out.println("isLoopbackAddress : " + ip1.isLoopbackAddress()); // isMCGlobal() method System.out.println("isMCGlobal : " + ip1.isMCGlobal()); // isMCLinkLocal() method System.out.println("isMCLinkLocal : " + ip1.isMCLinkLocal()); // isMCNodeLocal() method System.out.println("isMCNodeLocal : " + ip1.isMCNodeLocal()); // isMCOrgLocal() method System.out.println("isMCOrgLocal : " + ip1.isMCOrgLocal()); // isMCSiteLocal() method System.out.println("isMCSiteLocal : " + ip1.isMCSiteLocal()); // isMulticastAddress() method System.out.println("isMulticastAddress : " + ip1.isMulticastAddress()); // isSiteLocalAddress() method System.out.println("isSiteLocalAddress : " + ip1.isSiteLocalAddress()); // hashCode() method System.out.println("hashCode : " + ip1.hashCode()); // equals() method System.out.println("ip1==ip2 : " + ip1.equals(ip2)); } } |
chevron_right
filter_none
Output :
Address : [52, 84, 102, -116] Host Address : 52.84.102.140 isAnyLocalAddress : false isLinkLocalAddress : false isLoopbackAddress : false isMCGlobal : false isMCLinkLocal : false isMCNodeLocal : false isMCOrgLocal : false isMCSiteLocal : false isMulticastAddress : false isSiteLocalAddress : false hashCode : 877946508 ip1==ip2 : false
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.
Recommended Posts:
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Quartet Class with Triplet 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 1
- Java.lang.Class class in Java | Set 2
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2



