java.net.InetAddress
About
InetAddress is a class in the java.net package that represents an IP address — either IPv4 or IPv6. It is used to identify hosts (computers, servers, etc.) on the Internet or a local network. We can use InetAddress to resolve hostnames to IP addresses and vice versa, or to get information about the local machine’s IP configuration.
Why It’s Important
In networking applications, we often need to work with IP addresses — either to connect to a server, identify a client, or bind a service to a specific interface. InetAddress provides a high-level abstraction over IP addresses, simplifying these operations.
It hides low-level byte-level IP address handling and makes tasks like DNS lookups easy using simple methods.
Key Methods
getByName(String host)
Returns the IP address of the given hostname.
getAllByName(String host)
Returns all IP addresses associated with a hostname.
getLocalHost()
Returns the IP address of the local machine.
getHostName()
Returns the hostname associated with this IP address.
getHostAddress()
Returns the textual representation of the IP address.
isReachable(int timeout)
Tests if the address is reachable within a timeout period.
equals(Object obj)
Compares two InetAddress objects for equality.
toString()
Returns the string representation in the format: hostname / IP.
Example
Last updated