In Java, InetAddress is a class that represents an Internet Protocol (IP) address. It is part of the java.net package and provides methods for working with IP addresses. The InetAddress class can be used to create instances representing both IPv4 and IPv6 addresses.
Here’s a brief overview of some key aspects of the InetAddress class:
- Creating Instances:
- You can create an
InetAddress instance by using static factory methods like getByName() or getLocalHost().
- Example:
java
InetAddress address = InetAddress.getByName("www.example.com");
- Getting Information:
- The class provides methods to retrieve information about the IP address, such as its host name (
getHostName()), its IP address in byte array form (getAddress()), and its textual representation (toString()).
- Example:
java
String hostName = address.getHostName();
byte[] ipAddress = address.getAddress();
String ipAddressString = address.toString();
- Utility Methods:
InetAddress also includes methods like isReachable() to check if the address is reachable, and getCanonicalHostName() to get the fully qualified domain name.
- Example:
java
boolean reachable = address.isReachable(1000);
String canonicalHostName = address.getCanonicalHostName();
In summary, InetAddress is a Java class used to represent and work with IP addresses, providing methods for creating instances, retrieving information, and performing utility operations related to network addressing.