All for Joomla All for Webmasters

Java InetAddress class

Java InetAddress class represents an IP address. The class provides methods to get the IP of any host name for example,, etc.

Commonly used methods of InetAddress class

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

Let’s see a simple example of InetAddress class to get ip address of website.

  1. import*;
  2. import*;
  3. public class InetDemo{
  4. public static void main(String[] args){
  5. try{
  6. InetAddress ip=InetAddress.getByName(“”);
  7. System.out.println(“Host Name: “+ip.getHostName());
  8. System.out.println(“IP Address: “+ip.getHostAddress());
  9. }catch(Exception e){System.out.println(e);}
  10. }
  11. }


Host Name:
IP Address:
submit to reddit

Leave a Reply