Java InetAddress class

Let’s learn Java InetAddress class.

Java InetAddress class

InetAddress class represents an Internet Protocol (IP) address.

An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built.

Also read – abstraction in java

The IP address architecture is defined by RFC 790:Assigned Numbers, RFC 1918:Address Allocation for Private Internets, RFC 2365:Administratively Scoped IP Multicast, and RFC 2373: IPVersion 6 Addressing Architecture.

An instance of an InetAddress consists of an IP address and possibly its corresponding host name (depending on whether it is constructed with a host name or whether it has already done reverse host name resolution).

Here’s an example on java inetaddress.

import java.net.InetAddress;
public class JavaInetaddress
{
   public static void main(String[] args)
   {
      try
      {
         InetAddress ip = InetAddress.getByName("www.google.com");
         System.out.println("Host name: " + ip.getHostName());
         System.out.println("IP address: " + ip.getHostAddress());
      }
      catch(Exception ex)
      {
         System.out.println(ex);
      }
   }
}


Output:

Host name: www.google.com
IP address: 172.217.163.164


Reference – oracle help center