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: testing weather one object is greater than another object

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default testing weather one object is greater than another object

    Hey everyone, I have to write a compare method that takes two parameters. I would like to know if I can compare one object with another by comparing if it is greater or less than the other one. I know its possible to test if two objects are equal so I assume it could be done!

    Thanks!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: testing weather one object is greater than another object

    In general references to objects can be compared with == or !=, that is they do or do not refer to the same thing. And that's all: there is no well defined concept of reference value ordering. (as there might be if references were pointers to memory locations as they are in some other languages.)

    -----

    That's references, but what about the things they reference? These things (instances of String, Double, Point etc) have an equals() method that will test whether the things should be considered equal. And they sometimes have a method that allows one thing to be compared to another. Classes that do have a method allowing their instances to be compared implement the Comparable interface. Some classes (like String and Double) do implement Comparable, and some (like Point) do not.

    It is also possible to write a class which implements the Comparator interface. Basically such a class would implement a method like the one you describe. You would write such a method yourself: ie there is no built in support for such comparisons (for the reason mentioned). Writing classes with such a method (ie that implement comparator) is discussed in Oracle's Tutorial in the Object ordering page of the Collections trail.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    u-will-neva-no (February 19th, 2012)

Similar Threads

  1. Your Favorite Weather?
    By snowguy13 in forum Totally Off Topic
    Replies: 22
    Last Post: February 29th, 2012, 06:28 PM
  2. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 07:20 AM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  5. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 04:34 PM