Write a java program to print the ip address

Write a java program to print the ip address. This is a Java program that displays the system’s IP address. An Internet Protocol address is represented by the InetAddress class. To get the IP address string in textual format, we use this class’s getHostAddress() method. Here is the source code for the Java program that displays the system’s IP address.

import java.net.InetAddress;
public class Main
{
public static void main(String args[]) throws Exception
{
InetAddress IP = InetAddress.getLocalHost();
System.out.println("IP address= "+IP.getHostAddress());
}
}

 

Output:

IP address 000.000.000.000

What is IP address in computer

An IP address (Internet Protocol address) is numerical identification assigned to each device connected to computer network that communicates using the Internet Protocol. 
An IP address is used for two purposes: identifying host or network interface, and addressing specific location.

What is INET address in java program

The InetAddress class contains methods for obtaining the IP address of any host name, such as www.noteshippo.com, www.google.com, www.facebook.com, and so on. A 32-bit or 128-bit unsigned number is used to represent an IP address. The IP address and its accompanying host name are represented by an instance of InetAddress.

The Java InetAddress class’s getLocalHost() function returns an instance of InetAddress with the local host name and address. First, the system’s host name is retrieved, and then that name is resolved into InetAddress.