📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:26.825000             🧑  作者: Mango

Java Java类

此类扩展 InetAddress 类并表示 IPv4 地址。它提供了解释和显示有关 IP 地址的有用信息的方法。

此类的方法以 4 种格式接受输入:

  1. dddd:当使用这种格式作为输入时,每个给定的值从左到右分配给 IP 地址的 4 个字节。
  2. ddd:当这种格式用作输入时,最后一部分被解释为一个 16 位的数字,并分配给最右边的 2 个字节作为主机地址。这通常用于指定 B 类地址。
  3. dd:当这种格式被用作输入时,最后一部分被解释为一个 24 位的数字,并分配给最右边的 3 个字节作为主机地址。这通常用于指定 A 类地址。
  4. d:当使用这种格式作为输入时,给定的值直接存储为网络地址,无需任何重排。

方法 :

MethodsDescription
equals(Object obj)This method compares this object against the specified object.
getAddress()This method returns the raw IP address of this InetAddress object.
getHostAddress()This method returns the IP address string in the textual presentation form.
hashCode()This method returns a hashcode for this IP address.
isAnyLocalAddress()This method utility routine check if the InetAddress is a wildcard address.
isLinkLocalAddress()This method utility routine check if the InetAddress is a link-local address.
isLoopbackAddress()This method utility routine check if the InetAddress is a loopback address.
isMCGlobal()This method utility routine check if the multicast address has a global scope.
isMCLinkLocal()This method utility routine check if the multicast address has a link scope.
isMCNodeLocal()This method utility routine check if the multicast address has node scope.
isMCOrgLocal()This method utility routine to check if the multicast address has organization scope.
isMCSiteLocal()This method utility routine check if the multicast address has site scope.
isMulticastAddress()This method utility routine check if the InetAddress is an IP multicast address.
isSiteLocalAddress()This method utility routine check if the InetAddress is a site-local address.

Java实现:

Java
// Java program to illustrate various
// Inet4Address class methods
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
 
public class inet4add
{
    public static void main(String args[]) throws UnknownHostException
    {
        String url = "www.geeksforgeeks.org";
        Inet4Address ip1 = (Inet4Address) Inet4Address.getByName(url);
        Inet4Address ip2 = (Inet4Address) InetAddress.getByName("www.yahoo.com");
         
        // Following methods checks the property of the thus created object.
        // getAddress() method
        System.out.println("Address : " + Arrays.toString(ip1.getAddress()));
 
        // getHostAddress() method
        System.out.println("Host Address : " + ip1.getHostAddress());
 
        // isAnyLocalAddress() method
        System.out.println("isAnyLocalAddress : " + ip1.isAnyLocalAddress());
         
        // isLinkLocalAddress() method
        System.out.println("isLinkLocalAddress : " + ip1.isLinkLocalAddress());
         
        // isLoopbackAddress() method
        System.out.println("isLoopbackAddress : " + ip1.isLoopbackAddress());
         
        // isMCGlobal() method
        System.out.println("isMCGlobal : " + ip1.isMCGlobal());
         
        // isMCLinkLocal() method
        System.out.println("isMCLinkLocal : " + ip1.isMCLinkLocal());
         
        // isMCNodeLocal() method
        System.out.println("isMCNodeLocal : " + ip1.isMCNodeLocal());
         
        // isMCOrgLocal() method
        System.out.println("isMCOrgLocal : " + ip1.isMCOrgLocal());
         
        // isMCSiteLocal() method
        System.out.println("isMCSiteLocal : " + ip1.isMCSiteLocal());
         
        // isMulticastAddress() method
        System.out.println("isMulticastAddress : " + ip1.isMulticastAddress());
 
        // isSiteLocalAddress() method
        System.out.println("isSiteLocalAddress : " + ip1.isSiteLocalAddress());
 
        // hashCode() method
        System.out.println("hashCode : " + ip1.hashCode());
         
        // equals() method
        System.out.println("ip1==ip2 : " + ip1.equals(ip2));
    }
}


输出 :

Address : [52, 84, 102, -116]
Host Address : 52.84.102.140
isAnyLocalAddress : false
isLinkLocalAddress : false
isLoopbackAddress : false
isMCGlobal : false
isMCLinkLocal : false
isMCNodeLocal : false
isMCOrgLocal : false
isMCSiteLocal : false
isMulticastAddress : false
isSiteLocalAddress : false
hashCode : 877946508
ip1==ip2 : false