Proxy equals(Object) in Java

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

Syntax:

boolean java.net.Proxy.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 proxy as this object.

Two instances of Proxy represent the same address if both the SocketAddresses and type are equal.

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

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.UnknownHostException;

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

        InetAddress addr = InetAddress.getByName("java.sun.com");
        int port = 80;
        SocketAddress sockaddr = new InetSocketAddress(addr, port);
        Proxy proxy = new Proxy(Type.HTTP, sockaddr);

        System.out.println(proxy.equals(proxy));

    }
}

Output:

true


Some other methods of Proxy class

Proxy(Type, SocketAddress)This method creates an entry representing a PROXY connection. 

address()This method returns the socket address of the proxy, or null if it's a direct connection.

Proxy ConstantsSome of the Proxy constants like Proxy.NO_PROXY, Type.DIRECT, etc.

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

hashCode()This method returns a hashcode for this Proxy.

toString()This method constructs a string representation of this Proxy. This String is constructed by calling toString() on its type and concatenating " @ " and the toString() results from its address if its type is not DIRECT.

type()This method returns the proxy type.

No comments:

Post a Comment