Java Program to display the IP address of the system

Here we are explaining how to write a java program to display the IP address of the system.

What is an IP address?

An IP address is an Internet Protocol address. It is an identifying number associated with a computer.

Example:168.212.226.204

How to implement the java program to display the IP address of the system?

First, we have to declare the class IP. Then create an object of the InetAddress class as ip.Display the IP address in textual representation by using getHostAddress().The getLocalHost() function is used to get the localhost address. 

ALGORITHM

STEP 1: Declare the class IP with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Create an object of InetAddress class as ip.

STEP 4: Then display the IP address as ip. getHostAddress().

 

Java Source Code

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

OUTPUT

IP address of this system is := 127.0.0.1