Java InetAddress
Resolve and represent IP addresses in Java with the InetAddress class.
Java InetAddress
Every connection in this part needed an address — a host and a port. java.net.InetAddress is the class that represents the host part: an IP address, optionally paired with a hostname. It is also the gateway to DNS: turning a name like example.com into the numeric address the network actually routes to. This final Networking chapter covers creating, resolving, and inspecting addresses.
No public constructor — use factory methods
You never write new InetAddress(...). Instead:
InetAddress a = InetAddress.getByName("example.com"); // DNS lookup (or parse a literal)
InetAddress b = InetAddress.getByName("93.184.216.34"); // numeric literal: no DNS
InetAddress[] all = InetAddress.getAllByName("example.com"); // every address for a host
InetAddress lo = InetAddress.getLoopbackAddress(); // 127.0.0.1 / ::1, never fails
InetAddress me = InetAddress.getLocalHost(); // this machine's address
InetAddress c = InetAddress.getByAddress(new byte[]{10,0,0,1}); // from raw bytesKey distinction: getByName with a hostname performs a DNS query (which can be slow or throw UnknownHostException offline); with a numeric literal it just parses, no network. getByAddress builds an address from raw bytes with no lookup at all.
IPv4 and IPv6
InetAddress is the common supertype; concrete instances are Inet4Address (32-bit, 93.184.216.34) or Inet6Address (128-bit, ::1). getAddress() returns the raw bytes (4 or 16), and getHostAddress() returns the canonical text form. Code should treat both families uniformly through the InetAddress type.
Inspecting an address
Useful predicates classify an address without any network call: isLoopbackAddress(), isSiteLocalAddress() (private ranges like 10.x / 192.168.x), isMulticastAddress(), isAnyLocalAddress() (the wildcard 0.0.0.0), and isReachable(timeout) (an actual ping-style probe — the one method here that does hit the network).
A worked example: building and classifying addresses
This program creates addresses several ways — loopback, an IPv4 literal, raw bytes, an IPv6 literal — and inspects each with the classification predicates. It deliberately uses literals and loopback so it runs offline; the one DNS-dependent call (getLocalHost) is guarded.
What to take from the run:
- Addresses were created without a constructor — every instance came from a factory:
getLoopbackAddress,getByName,getByAddress. That design lets the JVM cache lookups and return the right subclass (Inet4AddressvsInet6Address) for you. getByName("93.184.216.34")andgetByName("::1")did no DNS query because the arguments were numeric literals — the method just parsed them. The same call with a hostname would have hit DNS, which is the slow, fail-on-offline path; knowing which inputs trigger a lookup matters for performance.- The raw byte count distinguished the families: the IPv4 address reported 4 bytes, the IPv6 address 16, and
instanceof Inet6Addressconfirmed the concrete type. Code that handles addresses generically works through theInetAddresssupertype and rarely needs to branch on the family. - The classification predicates answered structural questions with no network: the loopback address tested
trueforisLoopbackAddress(), and the10.0.0.1built from bytes testedtrueforisSiteLocalAddress()(a private range). These checks are pure arithmetic on the address bytes. getLocalHost()was wrapped in a try/catch because it can fail withUnknownHostExceptionwhen the machine's own name is not resolvable. Address resolution is fundamentally a network/DNS operation that can fail, so robust code always anticipates the unresolved case — the same caution every chapter in this part applied to the network itself.
Practice
A latency-sensitive service calls 'InetAddress.getByName(host)' in a tight loop, where 'host' is sometimes a domain name and sometimes a numeric IP literal. Profiling shows occasional multi-hundred-millisecond stalls. What is the best explanation?