Java API Tutorial - Java InetAddress.getHostAddress()








Syntax

InetAddress.getHostAddress() has the following syntax.

public String getHostAddress()

Example

In the following code shows how to use InetAddress.getHostAddress() method.

// w w w .  ja v a2s.  c o  m

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {

  public static void main(String[] args) {

    try {
      InetAddress me = InetAddress.getLocalHost();
      String dottedQuad = me.getHostAddress();
      System.out.println("My address is " + dottedQuad);
    } catch (UnknownHostException e) {
      System.out.println("I'm sorry. I don't know my own address.");
    }

  }

}

The code above generates the following result.