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

Learn Java - Java tutorial - Java inetaddress - Java examples - Java programs
- Java InetAddress class represents an IP address.
- The java.net.InetAddress class provides methods to get the IP of any host name for example www.wikitechy.com, www.google.com, www.facebook.com etc.
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. |
Sample Code
- The example is given below,
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.wikitechy.com");
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
Output
Host Name: www.wikitechy.com
IP Address: 192.169.231.100