Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why is comparison of objects of Class implementing CharSequence undefined?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is comparison of objects of Class implementing CharSequence undefined?

    Documentation says, This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map.

    Questions:
    1. Why is it necessary to refine equals and hashCode to ensure that objects of classes implementing CharSequence can be compared?
    2. What does it mean by each object may be implemented by a different class? Is it referring to:

    public final class A implements CharSequence{
    ...
    ...
    }

    public final class B implements CharSequence{
    ...
    ...
    }

    Class A a = new A();
    Class B b = new B();

    Boolean flag = (a == b); // is this what is undefined?

    Why is there a special mention about this fact in this class? This can be the fate for any other interface as well.
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is comparison of objects of Class implementing CharSequence undefined?

    An interface is not the same as a class. It has no code associated with it. It defines some requirements for classes that implement it.

Similar Threads

  1. calling objects in a differnt class
    By jack_nutt in forum Object Oriented Programming
    Replies: 12
    Last Post: July 8th, 2011, 01:57 PM
  2. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  3. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM
  4. Error with contains and CharSequence.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 09:19 AM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM