Implementation of equals, hashCode, and

I am writing an RMI application where the implementation of the remote interface cannot extend UnicastRemoteObject because it already extends another class. So, I need to explicitly export the server object using UnicastRemoteObject.exportObject(server, 0);� no problem.
Problem is I�ve read that in order to make the class behave as if it had extend UnicastRemoteObject, you must provide implementations for equals, hashCode, and toString. I�ve searched the world over for implementations of these methods equivalent to those found in UnicastRemoteObject� no luck so far.
Could you help me out?
I�ve found some code that I�ve listed below� Is it equivalent to the equals and hashCode method found in UnicastRemoteObject? What about toString()?
public class RemoteDataServer extends LocalDataServer implements RemoteInterface{
    private Remote serverStub;
    public RemoteDataServer() throws IOException {
        super();
        //export remote object
        serverStub = UnicastRemoteObject.exportObject(this);
    public boolean equals(Object obj) {
        return (obj == this || serverStub.equals(obj));
    public int hashCode() {
        return serverStub.hashCode();
//toString()???

Hi,
At book Effective Java by Joshua Bloch you can see some info
overriding these members.
See at http://java.sun.com/docs/books/effective/index.html
Here is the toString()spec method from Object :
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
but toString from class String simply returns the string contents of
an object.
Hope this help

Similar Messages

  • Implementing hashcode and equals

    Is there anything more than performance reasons to always implement hashCode when you implement equals? If I understand correctly, when you put items in a hashtable it chooses the bucket by the hashcode and checks there is no equal item with the equals method. So it should still work if you never implement the hashcode method. But is there any other reason to implement hashCode than performance? Or have I just understood this completely wrong?

    Everything you say is true. And your example shows that point well. However, what if I just implement a hashCode that always returns 0? How will this work then? And does it have any differences than performance?
    Here is an example
    import junit.framework.*;
    import java.util.*;
    public class EqualsTest extends TestCase
      public void testHashCode()
        Map hashTable = new Hashtable();
        hashTable.put( new TestItem( 0 ), "0" );
        hashTable.put( new TestItem( 1 ), "1" );
        assertEquals( 2, hashTable.size() );
        Set hashSet = new HashSet();
        hashSet.add( new TestItem( 0 ));
        hashSet.add( new TestItem( 1 ) );
        assertEquals( 2, hashSet.size() );
      private class TestItem
        private final int i;
        public TestItem( int i )
          this.i = i;
        public boolean equals( Object o )
          final TestItem testItem = ( TestItem ) o;
          return i == testItem.i;
        public int hashCode()
          return 0;
    [/code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • NotFilter doesn't have equals/hashCode implementation

    Hi Guys,
    I'm using 3.4.2/411 and NotFilter doesn't have an equals/hashCode implementation - this means that it doesn't get properly removed when you unregister a listener/CQC.
    It's easy to work around by extending the class, but it would be nice to have a proper fix.
    I'm also suspicius of InFilter...
    Thanks, Paul

    Thanks for your note.
    This issue has been filed as COH-2586 for future reference.

  • When we override Hashcode and Equal Methods

    Hi....I have doubt regading Hashcode and equal methods
    why we override this two methods....
    why we override hashcode method when we are overriding equal method,
    i would very thankful to give answser
    Thank you
    Ramesh

    hash code is computed to check the equality of two
    objects ,
    that is why if u change the default equal method
    implementation , u need to change the hashcode method
    as well.That's an incomplete answer at best.
    The hashcode method is used by hashing algorithms and data strcutures such as HashMap. This value is used to determine what 'bucket' the reference will go into. It cannot, by itself determine equality.
    If you are still unsure, I suggest looking up 'hash table' on google. I'm sure there's a decent explanation on wikipedia.

  • Use of hashCode and equals method in java(Object class)

    What is the use of hashCode and in which scenario it can be used?similarly use of equals method in a class when it overides form Object class. i.e i have seen many scenario the above said method is overridden into the class.so why and in which scenario it has to override?Please help me.

    You could find that out easily with google, that is a standard junior developer interview question.

  • SCJP 5.0 A question about hashcode() and equals()

    Here's an exercise from a book.
    Given:
    class SortOf {
    String name;
    int bal;
    String code;
    short rate;
    public int hashCode() {
    return (code.length()*bal);
    public boolean equals(Object o) {
    //insert code here
    Which of the following will fulfill the equals() and hashCode() contracts for this
    class? (Choose all that apply)
    A return ((SortOf)o).bal == this.bal;
    B return ((SortOf)o).code.length() == this.code.length();
    C return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length()*this.bal;
    D return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate ==
    this.code.length()*this.bal*this.rate;
    C is a correct answer but D is also a correct answer according to the book writer,
    and I don't understand why.
    Normally, the rule is that if two objects are equals (according to the equals
    method)) then they must return the same hashcode. If we have two objects A and B;
    and A.bal == 2, A.code.length()==3, A.rate == 4 and B.bal == 4, B.code.length() ==
    3 and B.rate == 2. A and B are equals according with the D answer but they do not
    return the same hashcode.
    May someone help me make it clear?

    Generally ,the instance members decides the equals( ) & hashCode( ) contract . Here the code.length , bal fields are generating the hashCode.
    So it is appropriate to override the equals( ) method in such a way that , thonly those two fields wil decid the equality of the objects .
    Thanks,
    laksh.

  • Method Overriding-toString(),hashCode() and equals()?

    Hi,
    In some Java Classes, I saw that
    toString()
    hashCode()
    equals()
    these 3 methods of Object Class is overloaded.
    I don't understand why and in what situation we have to override these 3 methods of Object Class.
    Thanks in advance for ur Knowledge Sharing.
    Regards,
    S.Prabu

    toString() is typically overridden in subclasses so as to output something useful about a class like instance variable values instead of just a hashvalue representing the object reference as per Object.
    hashCode() and equals() are overridden typically for use within the collections framework though you need to fully understand the hashing contract before starting to override these as they can cause strange results if not overridden correctly.
    Take a look at collections and specifically the hashing functionality of HashMap and HashSet for more info on these.

  • Help need to know hashcode() and Equals method

    Hi ,
    Now i'm working under hashtable functions. In that all are asking me to overwrite hashCode and equals method. Why?
    Please let me know briefly...........

    jverd wrote:
    euph wrote:
    jverd wrote:
    euph wrote:
    REFTY5_ wrote:
    Hi ,
    Now i'm working under hashtable functions. In that all are asking me to overwrite hashCode and equals method. Why?
    Please let me know briefly...........Thery're asking you to override the equals and hashCode methods. Override means replace.So does "overwrite." However, as you point out, "override" is the correct word here.So you think "overwrite" means the same as "replace"?To the same extent that "override" does. But both are in the general sense. Override has a specific meaning in this Java context, and overwrite does not.Don't tell me. That's what I said in my original reply.
    To my ear it doesn't. To me "overwrite" means the original is destroyed, whereas when something is "replaced" the fate of the original is unknown. So I think there's a slight semantic difference between these words.Fair enough.Fair enougth, but the next time I suggest you're better sorted.

  • Do I need to override enum's hashCode() and equals()?

    Hi all:
    I have a enum type let's say it is
    public enum Type
      ONE,
      TWO;
    }If I want to compare the enum value to see if they are same value or not, let's say I have two:
    Type a = Type.ONE;
    Type b = Type.TWO;should I use a.equals(b) or (a == b) If I want to use it as another class's key field, and I want to override this class hashCode() and equals() methods.
    Can enum Type return the correctly hash code?
    For example the class is like:
    public Class A
      Type type;
      pubilc boolean equals(Object other)
        // here skip check class type, null and class cast
        if(other.type.equals(this.type))
           return true;
        else
         return false;
      public int hashCode()
        int result = 31;
        result += 31 * result + type.hashCode() ;
       // can we get correct hash code from enum here?
        return result;
    }

    Answered by the test code by myself.
    We can use either equals or (==) to see if they are the same value.
    The enum's hashCode() use System.identityHashCode() to generate the hashCode and they are always correct

  • Implementation of Equals - ???

    The spec says:
    for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    Suppose, if class A is a subclass of class B,
    ob1 is an object of class A and ob2 is an object of class B,
    then is it necessary that
    if ob1.equals(ob2) is true, then
    ob2.equals(ob1) should also be true ???

    Also remember to override the hashCode().
    I think it would be more correct to say that ob1.equals(ob2) and ob2.equals(ob1) is false, since they are instantiated from different classes, even though class A is a subclass of class B. Usually you would just check if ob2 is an instance of A when you compare ob1 with ob2, and viceversa instance of B, in which case the first would be false and the second would be true, but that is not correct to do, since x.equals(y) should equal y.equals(x). You could override hashCode in class A so it is different from that calculated by B, because the Collection classes will first check the hashCode, then the equals method, to check whether to elements are the same.

  • Implement SSL in JSP and servlets

    hi there,
    i needed some help in implementing ssl in JSPs and servlets. My idea was to have a login page and also use it where sensitive data is being sent. I have used the basic socket programming (socket.class and serverSocket.class) in GUI applications using threads to listen and send simultaniously and understand how it works but how do I implement it in JSP and servlets and where do i store the certificate?
    i do know that using SSL sockets is very much the same as using the normal sockets.
    can anyone tell me any useful links or give me a step by step guide how to implement it and which classes to use
    your help would be very much appreciated.
    mani

    hi,
    soory for late reply.
    i think you need to use keytool program in your JDK to generate a certificate signature file and you need to configure the server.xml to open and use the HTTPS port. I have not had any additional info on this but i would assume that you simply enter https:// on the file you wish to load in your form and it should use the HTTPS port provided it can find the certificate which must be stored somewhere in your tomcat web server. i would recommend you see tomcat website or search on google for "ssl support in tomcat".i hav not had time to look into this but i will as soon as i am free from my work load.
    mani

  • Equal weighting  AND   Unequal weighting  in Vendor Evaluation ?

    Hi Experts,
    Can u pls explain me this small issue with Vendor Evaluation weighting
    SPRO -  IMG - MM  -  PURCHASING -  VENDOR EVALUATION  -  Maintain Purchasing Organization Data - SELECT PURCH .ORG -
    DOUBLE CLICK ON Weighting -  THERE IS I AM SEEING THIS LIST BELOW  -  Equal weighting  AND   Unequal weighting
    REPETING. my client wants  vendor evaluation   Unequal . so i have to delet the  Equal weighting  5 line or what to do ?
    can u pls explain me.
    01 Equal weighting 01 Price 1
    01 Equal weighting 02 Quality 1
    01 Equal weighting 03 fecha/cantidad 1
    01 Equal weighting 04 Serviceservicio 1
    01 Equal weighting 05 servicio 1
    02 Unequal weighting 01 Price 1
    02 Unequal weighting 02 Quality 1
    02 Unequal weighting 03 fecha/cantidad 2
    02 Unequal weighting 04 Serviceservicio 1
    02 Unequal weighting 05 servicio 1
    Thanksin Advance,
    Anthyodaya.

    Hi Priyanka,
    thank you for the reply . but finalley my question is
    my client wants  Unequal weighting  .
    so i have to delete that  Equal weighting lines or not .?
    i mean  line of below list  .which is in BOLD letters. ?
    01 Equal weighting 01 Price 1
    01 Equal weighting 02 Quality 1
    01 Equal weighting 03 fecha/cantidad 1
    01 Equal weighting 04 Serviceservicio 1
    01 Equal weighting 05 servicio 1
    02 Unequal weighting 01 Price 1
    02 Unequal weighting 02 Quality 1
    02 Unequal weighting 03 fecha/cantidad 2
    02 Unequal weighting 04 Serviceservicio 1
    02 Unequal weighting 05 servicio 1
    can u pls explain me.
    Thanksin Advance,
    Anthyodaya.

  • How the implementation differs between BW and BI , Is BI takes more time fo

    Hi All,
    I would like to know difference between implemenation and time lines for MM as mentioned below.
    How the implementation differs between BW and BI , Is BI takes more time for implementing MM module  than on BW?
    Thanks in advanced. (Full points will be awarded)
    With Regards,
    PCR

    Hi Timo,
    Thanks for response!
    But as i read from the following url: http://docs.oracle.com/cd/E15051_01/apirefs.1111/e10653/oracle/jbo/ViewObject.html, the setQueryTimeOut(int timeOutMills), the timeOut is mentioned in milliseconds. Please correct me if I am wrong.
    and i have overriden the executeQuery() method in the View Object Impl class as shown below:
    public void executeQuery() {
    Map sessionScope = ADFContext.getCurrent().getSessionScope();
    sessionScope.put("MyQuery", this);
    try {
    super.executeQuery();
    } finally {
    sessionScope.remove("MyQuery");
    throw new JboException("Query Taking too long to respond");
    and in the JAVA class i am calling the above method like this:
    monitor.setQueryTimeOut(6);
    monitor.executeQuery();
    But the issue is:
    1. The above exception message is getting carried forward to other pages as well. I mean somewhere in the session/ADFContext this message is being saved and error comes up/pops up when i click on other tabs of the page. How do i clear this?
    2. The above exception message is coming for the first time but when i click the 'Submit' button second time, i am getting the results and also the message that 'Query is taking too long to respond'. This should not be the case, everytime it should show the same message as the timeout limit is less and the query should end without fetching the results.
    Kindly let me know how to resolve the above issues, any pointers will be helpful.
    Thanks in advance.
    Edited by: user9223904 on Nov 3, 2012 4:42 AM

  • Difference between Object equals() method and ==

    Hi,
    Any one help me to clarify my confusion.
    stud s=new stud();
    stud s1=new stud();
    System.out.println("Equals======>"+s.equals(s1));
    System.out.println("== --------->"+(s==s1));
    Result:
    Equals ======> false
    == ------------> false
    Can you please explain what is the difference between equals method in Object class and == operator.
    In which situation we use Object equals() method and == operator.
    Regards,
    Saravanan.K

    corlettk wrote:
    I'm not sure, but I suspect that the later Java compilers might actually generate the same byte code for both versions, i.e. I suspect the compiler has gotten smart enough to devine that && other!=null is a no-op and ignore it... Please could could someone who understands bytecode confirm or repudiate my guess?Don't need deep understanding of bytecode
    Without !=null
    C:>javap -v SomeClass
    Compiled from "SomeClass.java"
    class SomeClass extends java.lang.Object
      SourceFile: "SomeClass.java"
      minor version: 0
      major version: 49
      Constant pool:
    const #1 = Method       #4.#15; //  java/lang/Object."<init>":()V
    const #2 = class        #16;    //  SomeClass
    const #3 = Field        #2.#17; //  SomeClass.field:Ljava/lang/Object;
    const #4 = class        #18;    //  java/lang/Object
    const #5 = Asciz        field;
    const #6 = Asciz        Ljava/lang/Object;;
    const #7 = Asciz        <init>;
    const #8 = Asciz        ()V;
    const #9 = Asciz        Code;
    const #10 = Asciz       LineNumberTable;
    const #11 = Asciz       equals;
    const #12 = Asciz       (Ljava/lang/Object;)Z;
    const #13 = Asciz       SourceFile;
    const #14 = Asciz       SomeClass.java;
    const #15 = NameAndType #7:#8;//  "<init>":()V
    const #16 = Asciz       SomeClass;
    const #17 = NameAndType #5:#6;//  field:Ljava/lang/Object;
    const #18 = Asciz       java/lang/Object;
    SomeClass();
      Code:
       Stack=1, Locals=1, Args_size=1
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
      LineNumberTable:
       line 1: 0
    public boolean equals(java.lang.Object);
      Code:
       Stack=2, Locals=2, Args_size=2
       0:   aload_1
       1:   instanceof      #2; //class SomeClass
       4:   ifeq    25
       7:   aload_1
       8:   checkcast       #2; //class SomeClass
       11:  getfield        #3; //Field field:Ljava/lang/Object;
       14:  aload_0
       15:  getfield        #3; //Field field:Ljava/lang/Object;
       18:  if_acmpne       25
       21:  iconst_1
       22:  goto    26
       25:  iconst_0
       26:  ireturn
      LineNumberTable:
       line 6: 0
    }With !=null
    C:>javap -v SomeClass
    Compiled from "SomeClass.java"
    class SomeClass extends java.lang.Object
      SourceFile: "SomeClass.java"
      minor version: 0
      major version: 49
      Constant pool:
    const #1 = Method       #4.#15; //  java/lang/Object."<init>":()V
    const #2 = class        #16;    //  SomeClass
    const #3 = Field        #2.#17; //  SomeClass.field:Ljava/lang/Object;
    const #4 = class        #18;    //  java/lang/Object
    const #5 = Asciz        field;
    const #6 = Asciz        Ljava/lang/Object;;
    const #7 = Asciz        <init>;
    const #8 = Asciz        ()V;
    const #9 = Asciz        Code;
    const #10 = Asciz       LineNumberTable;
    const #11 = Asciz       equals;
    const #12 = Asciz       (Ljava/lang/Object;)Z;
    const #13 = Asciz       SourceFile;
    const #14 = Asciz       SomeClass.java;
    const #15 = NameAndType #7:#8;//  "<init>":()V
    const #16 = Asciz       SomeClass;
    const #17 = NameAndType #5:#6;//  field:Ljava/lang/Object;
    const #18 = Asciz       java/lang/Object;
    SomeClass();
      Code:
       Stack=1, Locals=1, Args_size=1
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
      LineNumberTable:
       line 1: 0
    public boolean equals(java.lang.Object);
      Code:
       Stack=2, Locals=2, Args_size=2
       0:   aload_1
       1:   instanceof      #2; //class SomeClass
       4:   ifeq    29
       7:   aload_1
       8:   ifnull  29
       11:  aload_1
       12:  checkcast       #2; //class SomeClass
       15:  getfield        #3; //Field field:Ljava/lang/Object;
       18:  aload_0
       19:  getfield        #3; //Field field:Ljava/lang/Object;
       22:  if_acmpne       29
       25:  iconst_1
       26:  goto    30
       29:  iconst_0
       30:  ireturn
      LineNumberTable:
       line 6: 0
    }

  • Implementing  jdbc using jsp and servlets

    please give me documnetation and few programs with code .
    implementing or using jdbc with servlets and jsp.

    please give me documnetation and few programs with
    code .
    implementing or using jdbc with servlets and jsp.Well, which do you want to do? Implement JDBC with servlets and JSP - a tricky job, but there's no technical reason why you couldn't for instance write a class which both extends HttpServlet and implements java.sql.Driver. Wouldn't recommend it, though

Maybe you are looking for

  • How to prevent Copy from Cancelled Sales Orders.

    Hi, I have a requirement as below. I want to stop users from Copying a Cancelled Sales Order Lines. Can anybody please let me know how I can achieve this. I tried using the processing constraints but couldn't succeed. Thanks in advance. Thanks Shekha

  • How can i write a sign string in VISA Write?

    Hello everybody. I would like someone to help me. I have an array, which is transformed (by Byte Array to String) in a string that reaches the VISA Write Serial Port. My problem is that when making the transformation with Byte Array to String, i  los

  • 9/17/2013 - AIR 3.9.0.880 Runtime and SDK Beta

    Adobe AIR Beta Channel Update This beta release provides access to the latest AIR runtime and SDK (with compiler) for Windows, Mac OS, iOS and Android. Below are some of the key features and benefits of AIR 3.9.  Please see our release notes for full

  • ITunes library file cannot be saved. You do not have enough access privileges for this operation.

    iTunes suddenly give me the above error message and doesn't register playcounts and imports. Who can help?

  • Notinited and ClassNotFoundException errors

    I hope someone can help me with the following problem: When I run my applet in a browser as a local computer, (meaning that the Applet is running on my computer rather than on the internet) it runs perfectly fine in any browser. However, when I try l