Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Determining the IP Address & Hostname of Local Computer in Java

  • Last Updated : 04 Dec, 2020

IP Address  Stands for Internet Protocol Address, is a numeric label that is assigned uniquely to each device connected within a computer network that uses the internet protocol. An IP address serves two principal functions:

  1. It identifies the host, or more specifically its network interface.
  2. It provides the location of the host in the network, and thus the capability of establishing a path to that host.

The IP address also specifies the format of the data packets that are sent and received among routers and end systems. The IP address space is managed globally by the Internet Assigned Numbers Authority (IANA), and by five regional Internet registries (RIRs) responsible in their designated territories for assignment to local Internet registries, such as Internet service providers (ISPs), and other end users. 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

IP versions:

Two versions of IP addresses are most commonly seen in internet addressing these days. They are the IPv4 and IPv6.

  • IPv4: IPv4 has a size of 32 bits, i.e., 4,294,967,296 addresses can be assigned using IPv4. IPv4 addresses are represented in dot-decimal notation, consisting of 4 decimal numbers separated by dots, each ranging from 0 to 255. Each part represents a group of 8 bits (an octet) of the address. i.e.
  • IPv6: In IPv6, the address size was increased from 32 bits in IPv4 to 128 bits, thus providing up to 2^128 (approximately 3.403×10^38) addresses. i.e. 2001:db8:0:1234:0:567:8:1

Host Name

Host name is a name given to a device on the internet. i.e., a hostname is how a device can be referred to as on the internet (just like the names of a human). Hostnames may be simple names consisting of a single word or phrase, or they may be structured. i.e. LAPTOP-MRRIH5PC

The package provides several useful classes and interfaces to deal with networking and the internet. The IP address and hostname of any local computer can be determined using the class.


// Java program to determine the IP address
// and host name of local computer
public class GFG {
    public static void main(String[] args) throws Exception
        // a variable of type InetAddress to store
        // the address of the local host
        InetAddress addr = InetAddress.getLocalHost();
        // Returns the IP address string in
        // textual presentation.
        System.out.println("Local HostAddress:  "
                           + addr.getHostAddress());
        // Gets the host name for this IP address.
        System.out.println("Local host name: "
                           + addr.getHostName());
Local HostAddress:
Local host name: localhost

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!