0
if(new Integer(1) == new Integer(1)) return true;

I need to code/implement this so that this test:

//door is a class and the constructor takes length, breadth, width
if(new Door(10,10,10) == new Door(10,10,10))

will return true.

Does the Java compiler have any interface for wrapper classes to get their value and compare them?

Or simply: how does one check that some object > other object (user defined object but not by some primitive value/wrapper class)?

Mat
  • 188,820
  • 38
  • 367
  • 383

2 Answers2

15

It doesn't work in Java:

if (new Integer(1) == new Integer(1)) {
    System.out.println("This will not be printed.");
}

You may be getting confused with autoboxing, which will reuse objects for small values (exact range is implementation-specific - see the bottom of JLS section 5.1.7):

Integer x = 1;
Integer y = 1;
if (x == y) { // Still performing reference equality check
    System.out.println("This will be printed");
}

The new operator always returns a reference to a new object, so new ... == new ... will always evaluate to false.

You can't overload operators in Java - normally for equality comparisons you should use equals (which you can override and overload in your own class) and implement Comparable<T> for ordering, then use compareTo.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • good.., but still using the equals() java says they are equal/not equal but it will not say whether they(obj1 ,obj2) are greater or less – user2419836 May 25 '13 at 08:28
  • @user2419836: Which is why I said that you need to implement `Comparable` and call `compareTo`. – Jon Skeet May 25 '13 at 08:29
  • @user2419836, it entirely depends on how you implement `equals()` and `hashcode`. – Ravi Trivedi May 25 '13 at 08:29
  • how we will identify that one object is greater or lesser than the other one ,.where that object is not wrapper class(like DOOR) – user2419836 May 25 '13 at 08:30
  • @user2419836: It's up to you to decide how the ordering should work - you need to write the `compareTo` method appropriately. – Jon Skeet May 25 '13 at 08:30
  • Either you do as @JonSkeet's suggested way of using `Comparator` or implementing your own `eauals` where you use your fields for comparison or something what is convenient to you – Ravi Trivedi May 25 '13 at 08:31
  • 2
    @RaviTrivedi: You *mustn't* use `equals` for ordering. Just because you know two values are unequal doesn't mean you know which is "smaller" than the other. – Jon Skeet May 25 '13 at 08:45
  • @user2419836: What do you mean? We have no idea how you want to compare two `Door` values, or what the numbers are meant to represent. You should read the documentation for `Comparable` and implement it yourself. – Jon Skeet May 25 '13 at 08:45
  • k.. i got it what your saying jon ,, thanks but i am trying to find whether is it possible to check two object that are greater are not by simple using "if" tats it.i also know that the "if" simple verifies the references. – user2419836 May 25 '13 at 08:54
  • @user2419836: `if` itself just checks whether a condition is true or false. The condition can be any `boolean` expression. So you'd normally use `if (x.compareTo(y) < 0)` to see if `x` refers to an object which is logically smaller than `y`. It's important to differentiate between the `if` statement and the condition you choose to use with it. – Jon Skeet May 25 '13 at 08:57
  • ,jon you have any work out websites for java concurrent package which hold a good explination.. ha – user2419836 May 25 '13 at 08:57
  • @JonSkeet, I agree. Which is why I suggested to implement `equals` using instance's int or string field values. – Ravi Trivedi May 25 '13 at 10:47
  • @RaviTrivedi: But that's not going to help if the OP *actually* wants an ordering comparison. You seemed to be suggesting it would be okay to use `equals` for ordering, which it's not. – Jon Skeet May 25 '13 at 12:57
  • @JonSkeet, that's correct. `equals` is not for ordering. `comparator` or `comparable` is needed. – Ravi Trivedi May 25 '13 at 14:35
2

== will compare values of "references for objects", not the "values of the objects" themselves.

Here is the good reference that will help you to clear how comparison works in java as well as how to implement what you need.

Eugene Loy
  • 11,677
  • 8
  • 47
  • 73
  • 2
    Well, `==` compares values, and the value of an object is its reference (not the underlying data within the object). This is why Java is "pass-by-value", and yet throws object references around everywhere ([see here](http://stackoverflow.com/questions/40480/is-java-pass-by-reference)). – ajp15243 May 25 '13 at 08:30
  • By "object" I mean "non-primitive" here. – Eugene Loy May 25 '13 at 08:32
  • I mean "non-primitive" as well. Read through the answers to the linked question in my previous comment. – ajp15243 May 25 '13 at 08:36
  • Yes, primitives and non-primitives both hold number values. In the case of non-primitives, what you get is object hashcode(non-real value). And in the case of primitives, you get real value. – Ravi Trivedi May 25 '13 at 08:42