Search...
[Link] Class in Java
Last Updated : 20 Dec, 2022
public class InetAddress extends Object implements Serializable:
The [Link] 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 :
1. Unicast — An identifier for a single interface.
2. 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
DSAused
Practice
for Problems C C++
the purpose. Java Methods
Factory Python JavaScript Data Sciencein Machine
are static methods a classLearning Courses
that return Linu
an object
of that class.
There are 5 factory methods available in InetAddress class -
Method Description
This method returns the instance of
public static InetAddress getLocalHost() throws
InetAddress containing the local
UnknownHostException
hostname and address.
This method returns the instance of
public static InetAddress getByName( String
InetAddress containing IP and Host name
host ) throws UnknownHostException
of host represented by host argument.
public static InetAddress[] getAllByName( This method returns the array of the
String hostName ) throws instance of InetAddress class which
UnknownHostException contains IP addresses.
public static InetAddress getByAddress( byte This method returns an InetAddress
IPAddress[] ) throws UnknownHostException object created from the raw IP address.
public static InetAddress getByAddress( String This method creates and returns an
hostName, byte IPAddress[] ) throws InetAddress based on the provided
Method Description
UnknownHostException hostname and IP address.
Below is the Java implementation of InetAddress class to demonstrate the use of factory
methods -
import [Link].*;
import [Link].*;
import [Link].*;
class GFG {
public static void main(String[] args)
throws UnknownHostException
{
// To get and print InetAddress of Local Host
InetAddress address1 = [Link]();
[Link]("InetAddress of Local Host : "
+ address1);
// To get and print InetAddress of Named Host
InetAddress address2
= [Link]("[Link]");
[Link]("InetAddress of Named Host : "
+ address2);
// To get and print ALL InetAddresses of Named Host
InetAddress address3[]
= [Link]("[Link]");
for (int i = 0; i < [Link]; i++) {
[Link](
"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
= [Link](IPAddress);
[Link](
"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 = [Link](
"[Link]", IPAddress2);
[Link](
"InetAddresses of Host with specified IP Address and hostname : "
+ address5);
}
}
Output
InetAddress of Local Host : localhost/[Link]
InetAddress of Named Host : /[Link]
ALL InetAddresses of Named Host : /[Link]
InetAddresses of Host with specified IP Address : /[Link]
InetAddresses of Host with specified IP Address and hostname :
[Link]/[Link]
InetAddress — Instance Methods :
InetAddress class has plenty of instance methods that can be called using the object.
The instance methods are -
Method Description
This function compares this object against the
equals(Object obj)
specified object.
This method returns the raw IP address of this
getAddress()
InetAddress object, in bytes.
This method returns the fully qualified domain
getCanonicalHostName()
name for this IP address.
getHostAddress() This method gets the IP address in string form.
This method returns the host name for this IP
getHostName()
address.
hashCode() This method gets a hashcode for this IP address.
This method utility routine to check if the
isAnyLocalAddress()
InetAddress is an unpredictable address.
This method utility routine to check if the
isLinkLocalAddress()
address is not linked to local unicast address.
This method used to check if the InetAddress
isLoopbackAddress()
represents a loopback address.
This method utility routine check if this address
isMCGlobal()
has a multicast address of global scope.
Method Description
This method utility routine check if this address
isMCLinkLocal()
has a multicast address of link-local scope.
This method utility routine check if the multicast
isMCNodeLocal()
address has node scope.
This method utility routine check if the multicast
isMCOrgLocal()
address has an organization scope.
This method utility routine check if the multicast
isMCSiteLocal()
address has site scope.
This method checks whether the site has
isMulticastAddress()
multiple servers.
This method tests whether that address is
isReachable(int timeout)
reachable.
isReachable(NetworkInterface netif, int This method tests whether that address is
ttl, int timeout) reachable.
This method utility routine check if the
isSiteLocalAddress()
InetAddress is a site-local address.
This method converts and returns an IP address
toString()
in string form.
Below is the Java implementation of InetAddress class to demonstrate the use of
instance methods -
import [Link].*;
import [Link].*;
import [Link].*;
class GFG {
public static void main(String[] args)
throws UnknownHostException
{
InetAddress address1
= [Link]("[Link]");
InetAddress address2
= [Link]("[Link]");
InetAddress address3
= [Link]("[Link]");
// true, as clearly seen above
[Link](
"Is Address-1 equals to Address-2? : "
+ [Link](address2));
// false
[Link](
"Is Address-1 equals to Address-3? : "
+ [Link](address3));
// returns IP address
[Link]("IP Address : "
+ [Link]());
// returns host name,
// which is same as IP
// address in this case
[Link](
"Host Name for this IP Address : "
+ [Link]());
// returns address in bytes
[Link]("IP Address in bytes : "
+ [Link]());
// false, as the given site
// has only one server
[Link]("Is this Address Multicast? : "
+ [Link]());
[Link]("Address in string form : "
+ [Link]());
// returns fully qualified
// domain name for this IP address.
[Link](
"Fully qualified domain name for this IP address : "
+ [Link]());
// hashcode for this IP address.
[Link]("Hashcode for this IP address : "
+ [Link]());
// to check if the InetAddress is
// an unpredictable address..
[Link](
"Is the InetAddress an unpredictable address? : "
+ [Link]());
}
}
Output
Is Address-1 equals to Address-2? : true
Is Address-1 equals to Address-3? : false
IP Address : [Link]
Host Name for this IP Address : [Link]
IP Address in bytes : [B@579bb367
Is this Address Multicast? : false
Address in string form : [Link]/[Link]
Fully qualified domain name for this IP address : [Link]
Hashcode for this IP address : 756424231
Is the InetAddress an unpredictable address? : false
Comment More info Campus Training Program
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise wit
Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Connect
Careers Community
Contact Us Videos
Corporate Solution Blogs
Campus Training Program Nation Skill Up
Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
AI, ML & Data Science Data Science
DevOps Programming Languages
CS Core Subjects DevOps & Cloud
Interview Preparation GATE
GATE Trending Technologies
School Subjects
Offline Centers Preparation Corner
Noida Aptitude
Bengaluru Puzzles
Pune GfG 160
Hyderabad DSA 360
Patna System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved