byte[] ipAddress = new byte[] {(byte)192, (byte)168, (byte)1, (byte)1}; InetAddress address = InetAddress.getByAddress(ipAddress); System.out.println(address.getHostAddress()); // prints "192.168.1.1"
String hostname = "www.google.com"; InetAddress[] addresses = InetAddress.getAllByName(hostname); for (InetAddress address : addresses) { System.out.println(address.getHostAddress()); }In this example, we use the getAllByName method to retrieve all IP addresses associated with the hostname "www.google.com". We then loop through the array of InetAddress objects and print out each IP address. The java.net package library provides functionality for networking operations such as sockets, URLs, and IP addresses.