java.net.InetAddress Class in Java

29 Mar, 2021
public class InetAddress extends Object implements Serializable:
The java.net.InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses. 
There are 2 types of addresses :
Unicast — An identifier for a single interface.Multicast — An identifier for a set of interfaces.
Unicast — An identifier for a single interface.
Multicast — An identifier for a set of interfaces.
InetAddress – Factory Methods :
The InetAddress class is used to encapsulate both, the numerical IP address and the domain name for that address. The InetAddress class has no visible constructors. The InetAddress class has the inability to create objects directly, hence factory methods are used for the purpose. Factory Methods are static methods in a class that return an object of that class.
There are 5 factory methods available in InetAddress class – 
Below is the Java implementation of InetAddress class to demonstrate the use of factory methods – 
Java
import java.io.*;import java.net.*;import java.util.*;  class GFG {    public static void main(String[] args)        throws UnknownHostException    {        // To get and print InetAddress of Local Host        InetAddress address1 = InetAddress.getLocalHost();        System.out.println("InetAddress of Local Host : "                           + address1);          // To get and print InetAddress of Named Host        InetAddress address2            = InetAddress.getByName("45.22.30.39");        System.out.println("InetAddress of Named Host : "                           + address2);          // To get and print ALL InetAddresses of Named Host        InetAddress address3[]            = InetAddress.getAllByName("172.19.25.29");        for (int i = 0; i < address3.length; i++) {            System.out.println(                "ALL InetAddresses of Named Host : "                + address3[i]);        }          // To get and print InetAddresses of        // Host with specified IP Address        byte IPAddress[] = { 125, 0, 0, 1 };        InetAddress address4            = InetAddress.getByAddress(IPAddress);        System.out.println(            "InetAddresses of Host with specified IP Address : "            + address4);          // To get and print InetAddresses of Host        // with specified IP Address and hostname        byte[] IPAddress2            = { 105, 22, (byte)223, (byte)186 };        InetAddress address5 = InetAddress.getByAddress(            "gfg.com", IPAddress2);        System.out.println(            "InetAddresses of Host with specified IP Address and hostname : "            + address5);    }}
InetAddress of Local Host : localhost/127.0.0.1
InetAddress of Named Host : /45.22.30.39
ALL InetAddresses of Named Host : /172.19.25.29
InetAddresses of Host with specified IP Address : /125.0.0.1
InetAddresses of Host with specified IP Address and hostname : gfg.com/105.22.223.186
InetAddress — Instance Methods :
InetAddress class has plenty of instance methods that can be called using the object. The instance methods are – 
Below is the Java implementation of InetAddress class to demonstrate the use of instance methods – 
Java
import java.io.*;import java.net.*;import java.util.*;  class GFG {    public static void main(String[] args)        throws UnknownHostException    {          InetAddress address1            = InetAddress.getByName("45.22.30.39");        InetAddress address2            = InetAddress.getByName("45.22.30.39");        InetAddress address3            = InetAddress.getByName("172.19.25.29");          // true, as clearly seen above        System.out.println(            "Is Address-1 equals to Address-2? : "            + address1.equals(address2));        // false        System.out.println(            "Is Address-1 equals to Address-3? : "            + address1.equals(address3));          // returns IP address        System.out.println("IP Address : "                           + address1.getHostAddress());        // returns host name,        // which is same as IP        // address in this case        System.out.println(            "Host Name for this IP Address : "            + address1.getHostName());          // returns address in bytes        System.out.println("IP Address in bytes : "                           + address1.getAddress());          // false, as the given site        //  has only one server        System.out.println("Is this Address Multicast? : "                           + address1.isMulticastAddress());          System.out.println("Address in string form : "                           + address1.toString());          // returns fully qualified        // domain name for this IP address.        System.out.println(            "Fully qualified domain name for this IP address : "            + address1.getCanonicalHostName());          // hashcode for this IP address.        System.out.println("Hashcode for this IP address : "                           + address1.hashCode());          // to check if the InetAddress is        // an unpredictable address..        System.out.println(            "Is the InetAddress an unpredictable address? : "            + address1.isAnyLocalAddress());    }}
Is Address-1 equals to Address-2? : true
Is Address-1 equals to Address-3? : false
IP Address : 45.22.30.39
Host Name for this IP Address : 45.22.30.39
IP Address in bytes : [B@579bb367
Is this Address Multicast? : false
Address in string form : 45.22.30.39/45.22.30.39
Fully qualified domain name for this IP address : 45.22.30.39
Hashcode for this IP address : 756424231
Is the InetAddress an unpredictable address? : false
Java-net-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java
Stack Class in Java