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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read