import java.net.*; public class LoopbackAddressExample { public static void main(String[] args) throws UnknownHostException { InetAddress loopback = InetAddress.getLoopbackAddress(); System.out.println(loopback); } }
import java.net.*; public class LocalHostExample { public static void main(String[] args) throws UnknownHostException { InetAddress localhost = InetAddress.getLocalHost(); System.out.println("IP address: " + localhost.getHostAddress()); System.out.println("Hostname: " + localhost.getHostName()); } }This code creates an instance of InetAddress using the getLocalHost() method and prints the IP address and hostname of the current machine to the console. This example also uses the getHostAddress() and getHostName() methods to get the IP address and hostname respectively. Package Library: This example uses the java.net package library.