InetAddress Class

Posted By on April 29, 2016


Download PDF
Java URL
Introduction to JDBC

Java InetAddress class represents an IP address.

The java.net.InetAddress class provides methods to get the IP of any host name.

The InetAddress class has no visible constructors.

To create an InetAddress object, you have to use one of the available factory methods.

Method Description
public static InetAddress getByName(String host) throws UnknownHostException it returns the instance of InetAddress containing LocalHost IP and name.
public static InetAddress getLocalHost() throws UnknownHostException it returns the instance of InetAdddress containing local host name and address.
public String getHostName() it returns the host name of the IP address.
public String getHostAddress() it returns the IP address in string format.

Example of Java InetAddress class

mport java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName(“www.w4e.in”);

System.out.println(“Host Name: “+ip.getHostName());
System.out.println(“IP Address: “+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}

Output:

Host Name: www.world4engineers.com
IP Address: x.x.x.x

Java URL
Introduction to JDBC

Download PDF