Java API Tutorial - Java InetAddress.getHostName()








Syntax

InetAddress.getHostName() has the following syntax.

public String getHostName()

Example

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

/*from   w  w  w .  j  a  v  a2  s  .c om*/
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {

  public static void main(String[] args) {

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

  }

}

The code above generates the following result.