import java.net.InetAddress;
class IPAddress
{
public static void main(String args[]) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}
Program Explanation:
This Java program illustrates how to get the local IP address of the machine (computer) on which the program is running using the InetAddress class from the java.net package.
Step-by-Step Breakdown:
1. Importing the InetAddress class:
import java.net.InetAddress;
The InetAddress class is located in the java.net package and has the methods for retrieval of the IP address of the machine that is running the program, as well as other network-related details.
2. Class Definition:
class IPAddress
A class called IPAddress is declared. This class contains the main method, which is the entry point for the program.
3. Main Method:
public static void main(String args[]) throws Exception
The main method is where the program execution begins. It is marked as throws Exception, which indicates that any exceptions thrown by the program (such as network-related exceptions) will be handled automatically.
4. Getting the Local Host IP Address:
System.out.println(InetAddress.getLocalHost());
- InetAddress.getLocalHost() is the method that gives a local address of the machine on which this program is being executed. The return type for the above code will be of InetAddress, which can represent an IP address and also its corresponding hostname for the local machine.
- System.out.println() will print out the returned object of InetAddress onto the console.
- The getLocalHost() method obtains the IP address associated with the local machine and its corresponding hostname. The associated hostname in most cases is either localhost or 127.0.0.1. These are used for communication purposes within the same local machine (loopback address).
What Does the Program Do in Execution?
- The program invokes the getLocalHost() method, which returns the IP address for the local machine usually represented by the loop-back address 127.0.0.1 for most systems.
- The InetAddress object returned by getLocalHost(), representing the IP address of the local machine is then printed to the console.
Expected Output:
Output looks like this:
/127.0.0.1
Output Explanation:
- 127.0.0.1: The loopback address refers to the local machine, and is widely used in tests and diagnostics on the network.
- It also might have output of a hostname of a machine, which can depend on OS and networking configurations.
For instance, depending on the operating system and your configuration, on some systems it can look something like this:
localhost/127.0.0.1
localhost is a hostname for a machine and the corresponding IP address is 127.0.0.1.
Key Points:
- InetAddress.getLocalHost(): It will return the local machine’s IP address.
- InetAddress: This class of Java is meant to represent an IP address, which can usually be found in the work done to network activities that involve tasks for resolving hostnames or retrieving an IP address.
Conclusion:
- It is a program showing how to obtain the local IP address of a machine running the program using Java’s InetAddress.
- It prints out the InetAddress object, generally containing the loopback address (127.0.0.1) and associated hostname (localhost).