java.net.InetAddress Class in Java
Last Updated :
20 Dec, 2022
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.
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 -
Method | Description |
---|
public static InetAddress getLocalHost() throws UnknownHostException | This method returns the instance of InetAddress containing the local hostname and address. |
public static InetAddress getByName( String host ) throws UnknownHostException | This method returns the instance of InetAddress containing IP and Host name of host represented by host argument. |
public static InetAddress[] getAllByName( String hostName ) throws UnknownHostException | This method returns the array of the instance of InetAddress class which contains IP addresses. |
public static InetAddress getByAddress( byte IPAddress[] ) throws UnknownHostException | This method returns an InetAddress object created from the raw IP address. |
public static InetAddress getByAddress( String hostName, byte IPAddress[] ) throws UnknownHostException | This method creates and returns an InetAddress based on the provided hostname and IP address. |
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);
}
}
OutputInetAddress 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 -
Method | Description |
---|
equals(Object obj) | This function compares this object against the specified object. |
getAddress() | This method returns the raw IP address of this InetAddress object, in bytes. |
getCanonicalHostName() | This method returns the fully qualified domain name for this IP address. |
getHostAddress() | This method gets the IP address in string form. |
getHostName() | This method returns the host name for this IP address. |
hashCode() | This method gets a hashcode for this IP address. |
isAnyLocalAddress() | This method utility routine to check if the InetAddress is an unpredictable address. |
isLinkLocalAddress() | This method utility routine to check if the address is not linked to local unicast address. |
isLoopbackAddress() | This method used to check if the InetAddress represents a loopback address. |
isMCGlobal() | This method utility routine check if this address has a multicast address of global scope. |
isMCLinkLocal() | This method utility routine check if this address has a multicast address of link-local scope. |
isMCNodeLocal() | This method utility routine check if the multicast address has node scope. |
isMCOrgLocal() | This method utility routine check if the multicast address has an organization scope. |
isMCSiteLocal() | This method utility routine check if the multicast address has site scope. |
isMulticastAddress() | This method checks whether the site has multiple servers. |
isReachable(int timeout) | This method tests whether that address is reachable. |
isReachable(NetworkInterface netif, int ttl, int timeout) | This method tests whether that address is reachable. |
isSiteLocalAddress() | This method utility routine check if the InetAddress is a site-local address. |
toString() | This method converts and returns an IP address in string form. |
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());
}
}
OutputIs 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
Similar Reads
Java.net.InterfaceAddress class in Java
This class represents a network interface address. Every device that has an IP address has an IP address on the network interface. In fact the ping command doesn't ping a device but the devices interface address. Java provides certain methods to deal with interface addresses which can be used in pla
2 min read
java.net.URL Class in Java
URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part
4 min read
Java.io.InputStream Class in Java
Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is i
3 min read
Java.net.JarURLConnection class in Java
Prerequisite - JAR files in Java What is a Jar file? JavaArchive(JAR) bundles all the classes in one package. Since the archive is compressed and can be downloaded in a single HTTP connection, it is often faster to download the archive than to download individual classes. Although jar bundles all th
4 min read
java.net.Proxy Class in Java
A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read
Java.lang.Enum Class in Java
Enum class is present in java.lang package. It is the common base class of all Java language enumeration types. For information about enums, refer enum in java Class Declaration public abstract class Enum<E extends Enum> extends Object implements Comparable, Serializable As we can see, that En
8 min read
java.nio.file.FileStore Class in Java
Java.nio.file is a package in java that consists of the FileStore class. FileStore class is a class that provides methods for the purpose of performing some operations on file store. FileStore is a class that extends Object from java.lang package. And few methods the FileStore class can inherit from
4 min read
java.net.Socket Class in Java
The java.net.Socket class allows us to create socket objects that help us in implementing all fundamental socket operations. We can perform various networking operations such as sending, reading data and closing connections. Each Socket object that has been created using with java.net.Socket class h
5 min read
Java.util.jar.JarEntry class in Java
This class is used to represent a JAR file entry. Constructors : JarEntry(JarEntry je) : Creates a new JarEntry with fields taken from the specified JarEntry object. JarEntry(String name) : Creates a new JarEntry for the specified JAR file entry name. JarEntry(ZipEntry ze) : Creates a new JarEntry w
2 min read
java.nio.file.Paths Class in Java
java.nio.file.Paths class contains static methods for converting path string or URI into Path. Class declaration : public final class Paths extends ObjectMethods: MethodDescriptionget(String first, String... more) This method converts a path string, or a sequence of strings that when joined form a p
2 min read