Skip to main content Skip to docs navigation
TechSpiderTutorials

java.net.Inet4Address Class in Java

java.net.Inet4Address Class

This class represents an Internet Protocol version 4 (IPv4) address.

Creating Objects

InetAddress.getByName("192.168.1.1"):

Retrieves the InetAddress object representing the IPv4 address "192.168.1.1".


InetAddress.getByName("127.0.0.1"):

Retrieves the InetAddress object representing the loopback IPv4 address "127.0.0.1".

java.net.Inet4Address Methods

public byte[] getAddress()

Returns the raw IP address of this InetAddress object.


public String getHostAddress()

Returns the IP address string in textual presentation form.


public int hashCode()

Returns a hashcode for this IP address.


public boolean isAnyLocalAddress()

Utility routine to check if the InetAddress in a wildcard address.


public boolean isLinkLocalAddress()

Utility routine to check if the InetAddress is an link local address.


public boolean isLoopbackAddress()

Utility routine to check if the InetAddress is a loopback address.


public boolean isMCGlobal()

Utility routine to check if the multicast address has global scope.


public boolean isMCLinkLocal()

Utility routine to check if the multicast address has link scope.


public boolean isMCNodeLocal()

Utility routine to check if the multicast address has node scope.


public boolean isMCOrgLocal()

Utility routine to check if the multicast address has organization scope.


public boolean isMCSiteLocal()

Utility routine to check if the multicast address has site scope.


public boolean isMulticastAddress()

Utility routine to check if the InetAddress is an IP multicast address.


public boolean isSiteLocalAddress()

Utility routine to check if the InetAddress is a site local address.


java.net.Inet4Address Class Example

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Inet4AddressExample {
    public static void main(String[] args) {
        try {
            // Create Inet4Address instances
    Inet4Address address1 = (Inet4Address) InetAddress.getByName("192.168.1.1");
    Inet4Address address2 = (Inet4Address) InetAddress.getByName("127.0.0.1");

            // Displaying IP addresses
    System.out.println("Address 1: " + address1.getHostAddress());
    System.out.println("Address 2: " + address2.getHostAddress());

            // Getting byte array representation of the IP address
            byte[] byteAddress = address1.getAddress();
            System.out.print("Byte Representation of Address 1: ");
            for (byte b : byteAddress) {
                System.out.print((b & 0xFF) + ".");
            }
            System.out.println();

            // Comparing two IPv4 addresses
            boolean isEqual = address1.equals(address2);
            System.out.println("Address 1 equals Address 2? " + isEqual);
        } catch (UnknownHostException e) {
            System.out.println("Unable to find address for given host");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

Address 1: 192.168.1.1
Address 2: 127.0.0.1
Byte Representation of Address 1: 192.168.1.1.
Address 1 equals Address 2? false