InterfaceAddress getNetworkPrefixLength() in Java

getNetworkPrefixLength(): This method is available in the java.net.InterfaceAddress class of Java.

Syntax:

short java.net.InterfaceAddress.getNetworkPrefixLength()

This method returns the network prefix length for this address. This is also known as the subnet mask in the context of IPv4 addresses.

Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)or 24 (255.255.255.0).

Parameters: NA

Returns: a short representing the prefix length for the subnet of that address.

Exceptions: NA

Approach

Java

package com.InterfaceAddress;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.List;

public class InterfaceAddressgetNetworkPrefixLength {
    public static void main(String[] args) throws SocketException {

        NetworkInterface nif = NetworkInterface.getByIndex(1);
        List<InterfaceAddress> list = nif.getInterfaceAddresses();

        InterfaceAddress interfaceAddress = list.get(0);

        System.out.println(interfaceAddress.getNetworkPrefixLength());
    }
}

Output:

8


Some other methods of InterfaceAddress

equals(Object)This method compares this object against the specified object. 

getAddress()This method returns an InetAddress for this address.

getBroadcast()This method returns an InetAddress for the broadcast address for this InterfaceAddress.

getNetworkPrefixLength()This method returns the network prefix length for this address.

hashCode()This method returns a hashcode for this Interface address.

toString()This method converts this Interface address to a String. The string returned is of the form: InetAddress / prefix-length [ broadcast address ].

No comments:

Post a Comment