InetAddress Literal Address Checks
Parse literal IP addresses without DNS and classify them with isLoopback, isSiteLocal, and isAnyLocal probes for validation code.
Code
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressChecks {
public static void main(String[] args) throws UnknownHostException {
// literal IPs parse without a DNS lookup; only hostnames need resolving
InetAddress loopback = InetAddress.getByName("127.0.0.1");
System.out.println(loopback + " loopback=" + loopback.isLoopbackAddress());
InetAddress v6 = InetAddress.getByName("::1");
System.out.println(v6 + " loopback=" + v6.isLoopbackAddress());
InetAddress site = InetAddress.getByName("192.168.1.10");
System.out.println(site + " siteLocal=" + site.isSiteLocalAddress());
InetAddress any = InetAddress.getByName("0.0.0.0");
System.out.println(any + " anyLocal=" + any.isAnyLocalAddress());
// getLoopbackAddress returns a reusable constant, no parsing per call
System.out.println("shared loopback: " + InetAddress.getLoopbackAddress().getHostAddress());
// malformed IPv4 literals throw UnknownHostException right away
try {
InetAddress.getByName("999.9.9.9");
} catch (UnknownHostException e) {
System.out.println("malformed literal rejected: " + e.getMessage());
}
}
}Inside the snippet
One way to read it: the class InetAddressChecks, doing all the work.
Using it
The demo output is labeled (shared loopback:, malformed literal rejected:); each label names the call that printed it.
Key statements
InetAddress loopback = InetAddress.getByName("127.0.0.1");
InetAddress site = InetAddress.getByName("192.168.1.10");
InetAddress any = InetAddress.getByName("0.0.0.0");
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.Email Address ValidatorValidate email syntax, checking the local part, domain labels, and top-level domain against common rules.MAC Address FormatterNormalize a MAC address from any separator style into colon, hyphen, Cisco dotted, and plain forms, with 12-hex-digit validation and case control.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.
FAQ
- What does InetAddress Literal Address Checks print when run?
- No setup needed: run the file and look for labeled lines like `shared loopback:`, `malformed literal rejected:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.