java tutorial - Java InetAddress class - java programming - learn java - java basics - java for beginners

Java inetaddress

Learn Java - Java tutorial - Java inetaddress - Java examples - Java programs

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

Methods of InetAddress class

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.

Sample Code

  • The example is given below,
public class InetDemo{  
public static void main(String[] args){  
InetAddress ip=InetAddress.getByName("");  
System.out.println("Host Name: "+ip.getHostName());  
System.out.println("IP Address: "+ip.getHostAddress());  
}catch(Exception e){System.out.println(e);}  
click below button to copy the code. By - java tutorial - team


Host Name:
IP Address:

Related Searches to Java InetAddress class