InetSocketAddress equals(Object) in Java

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

Syntax:

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

This method takes one argument. This method 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 address as this object.

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.InetSocketAddress;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

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

        int port = 8080;
        String hostname = "beingcodeexpert.blogspot.com";
        InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);

        InetAddress addr = InetAddress.getLocalHost();
        InetSocketAddress obj = new InetSocketAddress(addr, port);

        System.out.println(inetSocketAddress.equals(obj));
    }
}

Output:

false


Some other methods of InetSocketAddress class

InetSocketAddress.InetSocketAddress(int)This method creates a socket address where the IP address is the wildcard address and the port number a specified value.

InetSocketAddress.InetSocketAddress(InetAddress, int)This method creates a socket address from an IP address and a port number.

InetSocketAddress.InetSocketAddress(String, int)This method creates a socket address from a hostname and a port number.

InetSocketAddress.createUnresolved(String, int)This method creates an unresolved socket address from a hostname and a port number.

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

getAddress()This method gets the InetAddress.

getHostName()This method gets the hostname.

getHostString()This method returns the hostname, or the String form of the address if it doesn't have a hostname (it was created using a literal).

getPort()This method gets the port number.

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

isUnresolved()This method checks whether the address has been resolved or not.

toString()This method constructs a string representation of this InetSocketAddress.This String is constructed by calling toString() on the InetAddressand concatenating the port number (with a colon).

No comments:

Post a Comment