Inet4Address equals(Object) in Java

equals(Object): This method is available in the java.net.Inet4Address class of Java.

Syntax:

boolean java.net.Inet4Address.equals(Object obj)

Compares this object against the specified object. The result is true if and only if the argument is not null and it represents the same IP address as this object.

Two instances of InetAddress represent the same IP address if the length of the byte arrays returned by getAddress is the same for both, and each of the array components is the same for the byte arrays.

Parameters: One parameter is required for this method.

obj: the object to compare against.

Returns: true if the objects are the same; false otherwise.

Exceptions: NA

Approach

Java

package com.Inet4Address;

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

public class Inet4Addressequals {
    public static void main(String[] args)
throws UnknownHostException {

        String host = "beingcodeexpert.blogspot.com";
        Inet4Address inet4Address =
(Inet4Address) Inet4Address.getByName(host);

        System.out.println(inet4Address.equals(inet4Address));
    }
}

Output:

true


Some other methods of Inet4Address class

equals(Object)Compares this object against the specified object. The result is true if and only if the argument is not null and it represents the same IP address as this object.

getAddress()This method returns the raw IP address of this InetAddress object.

getHostAddress()This method returns the IP address string in textual presentation form.

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

isAnyLocalAddress()Utility routine to check if the InetAddress is a wildcard address.

isLinkLocalAddress()Utility routine to check if the InetAddress is a link-local address.

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

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

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

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

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

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

isMulticastAddress()Utility routine to check if the InetAddress is an IP multicast address. IP multicast address is a Class D address i.e first four bits of the address are 1110.

isSiteLocalAddress()Utility routine to check if the InetAddress is a site-local address.

IDN class in Java

java.net.IDN

Provides methods to convert internationalized domain names (IDNs) between a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation. Internationalized domain names can use characters from the entire range of Unicode. In contrast, traditional domain names are restricted to ASCII characters. ACE is an encoding of Unicode strings that uses only ASCII characters and can be used with software (such as the Domain Name System) that only understands traditional domain names.

Internationalized domain names are defined in RFC 3490.RFC 3490 defines two operations: ToASCII and ToUnicode. These 2 operations employ the Name prep algorithm, a profile of String prep, and the Puny code algorithm to convert domain name string back and forth.

The behavior of the aforementioned conversion process can be adjusted by various flags:

1. If the ALLOW_UNASSIGNED flag is used, the domain name string to be converted can contain code points that are unassigned in Unicode 3.2, which is the Unicode version on which IDN conversion is based. If the flag is not used, the presence of such unassigned code points is treated as an error.

2. If the USE_STD3_ASCII_RULES flag is used, ASCII strings are checked against RFC 1122 and RFC 1123.It is an error if they don't meet the requirements.

These flags can be logically OR'ed together.

Some methods of IDN class

1. IDN.ALLOW_UNASSIGNED

int java.net.IDN.ALLOW_UNASSIGNED: 1 [0x1]

Flag to allow processing of unassigned code points.

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNALLOW_UNASSIGNED {
    public static void main(String[] args) {

        System.out.println(IDN.ALLOW_UNASSIGNED);
    }
}

Output:

1


2. IDN.toASCII(String input)

String java.net.IDN.toASCII(String input)

Translates a string from Unicode to ASCII Compatible Encoding (ACE),as defined by the ToASCII operation of RFC 3490.

Parameters: One parameter is required for this method.

input: the string to be processed.

Returns: the translated String.

Throws:

1. IllegalArgumentException - if the input string doesn't conform to RFC 3490 specification.

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNtoASCII {
    public static void main(String[] args) {

        String input = "hello";
        System.out.println(IDN.toASCII(input));
    }
}

Output:

hello


3. IDN.toASCII(String input, int flag)

String java.net.IDN.toASCII(String input, int flag)

Translates a string from Unicode to ASCII Compatible Encoding (ACE), as defined by the ToASCII operation of RFC 3490.

ToASCII operation can fail. ToASCII fails if any step of it fails. If the ToASCII operation fails, an IllegalArgumentException will be thrown. In this case, the input string should not be used in an internationalized domain name.

Parameters: Two parameters are required for this method.

input: the string to be processed.

flag: process flag; can be 0 or any logical OR of possible flags.

Returns: the translated String.

Throws:

1. IllegalArgumentException - if the input string doesn't conform to RFC 3490 specification.

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNtoASCII2 {
    public static void main(String[] args) {

        String input = "hello";
        int flag = 1;
        System.out.println(IDN.toASCII(input, flag));
    }
}

Output:

hello


4. IDN.toUnicode(String input)

String java.net.IDN.toUnicode(String input)

Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the ToUnicode operation of RFC 3490.

Parameters: One parameter is required for this method.

input: the string to be processed.

Returns: the translated String

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNtoUnicode {
    public static void main(String[] args) {

        String input = "hello";
        System.out.println(IDN.toUnicode(input));
    }
}

Output:

hello


5.

String java.net.IDN.toUnicode(String input, int flag)

Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the ToUnicode operation of RFC 3490.

ToUnicode never fails. In case of any error, the input string is returned unmodified.

Parameters: Two parameters are required for this method.

input: the string to be processed.

flag: process flag; can be 0 or any logical OR of possible flags.

Returns: the translated String

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNtoUnicode2 {
    public static void main(String[] args) {

        String input = "hello";
        int flag = 1;
        System.out.println(IDN.toUnicode(input, flag));
    }
}

Output:

hello


6. IDN.USE_STD3_ASCII_RULES

int java.net.IDN.USE_STD3_ASCII_RULES: 2 [0x2]

Flag to turn on the check against STD-3 ASCII rules.

Approach

Java

package com.IDN;

import java.net.IDN;

public class IDNUSE_STD3_ASCII_RULES {
    public static void main(String[] args) {

        System.out.println(IDN.USE_STD3_ASCII_RULES);
    }
}

Output:

2