2
if(foo != bar)
    Log(foo + " is not equal to " + bar);

Prints:

FooBar@ca4c33db is not equal to FooBar@ca4c33db

This is happening on Android. FooBar is my own custom class. I double-checked it in the debugger.

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
  • I think `foo` is a string so you have to use .equals instead of `==`. – InnocentKiller Feb 07 '14 at 10:42
  • foo is not a String. I am trying to compare if foo and bar are the same object, which they are. – Barry Fruitman Feb 07 '14 at 10:43
  • 13
    Can you post a short but complete program demonstrating the problem? (Are you sure that in your original code you don't have an extra `;` at the end of your `if` statement, for example?) – Jon Skeet Feb 07 '14 at 10:45
  • @BarryFruitman - foo and bar are instances of which class? – TheLostMind Feb 07 '14 at 10:46
  • 1
    @TheLostMind of `FooBar` considering the way it prints. – assylias Feb 07 '14 at 10:47
  • 1
    Seems none of the answers are valid. – Abimaran Kugathasan Feb 07 '14 at 10:48
  • @assylias - I donno how I missed that.. thanks :) – TheLostMind Feb 07 '14 at 10:49
  • 3
    The truth is that with this information, it is very difficult to help. IF those objects are the same, the log should not be shown, but we cannot know what is happening if you only show this to us. – luanjot Feb 07 '14 at 10:49
  • 3
    Given the sparse information, you have two distinct instances of FooBar which accidently give the same output when their toString() method is invoked. Actually, without additional code, this is a rather poor question. – Gyro Gearless Feb 07 '14 at 10:53
  • 2
    Sorry if this is obvious, but have you overridden `toString()` or `hashCode()`? It's the only way I've been able to reproduce anything like what you're saying has happened. – Dan Temple Feb 07 '14 at 11:02
  • I also agree with @GyroGearless . It seems the two objects are deifferent but their toString method is somehow same. Are you, may be, delegating the toString call to a different object a reference to which might be shared by oth foo and bar? Are both foo and bar of type FooBar? – Satyan Raina Feb 07 '14 at 11:11
  • This cannot be. This should not happen. I propose to create a sandbox example that has this glitch. At first, let us use `Log.d(tag,string)` instead of `Log(x)`, we do not know how that function is defined. At second, how the class FooBar is defined? How its toString() method is defined? I can think out a case where a FooBar has a reference to another FooBar and forwards all messages to it, including toString(). – 18446744073709551615 Feb 07 '14 at 11:19
  • Sort of: `class X { X other; public String toString() { if(other!=null) return other.toString(); else return super.toString(); } public void doSomething() { if(other!=null) { other.doSomething(); return; } ... } }` – 18446744073709551615 Feb 07 '14 at 11:20
  • Is this a question about identity (as you use the operator) or about equality (as stated in the message)? Most answers look like reflex answers that answer the "why are my strings not the same" question where the code is about identity (so most "answers" plainly aren't answers to this question and are not useful at all). Without the relevant methods the question can't be answered now. – Hauke Ingmar Schmidt Feb 07 '14 at 14:24

3 Answers3

0

You should use equals()

if(!foo.equals(bar))

But with String you can do following since String class has implemented equals() method.

    String a = "hi";
    String b = "hii";
    if (a!=b){
        System.out.println("yes");
    }else {
        System.out.println("no");
    }

Out put:

   yes

If you want to go with your way you must override equals() method.

Example: With out override equals()

public class Test {
private String name;
private int age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
 }
}

Now take a look at this

   Test a = new Test();
    Test b = new Test();
    a.setAge(1);
    a.setName("hi");

    b.setAge(1);
    b.setName("hi");
    if (a!=b){
        System.out.println("yes");
    }else {
        System.out.println("no");
    }

Out put:

   yes

Now you can see same issue here.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
-1

There are a handful of things to note on this question.

Normally == is not accurate since it compares "memory addresses" (or as near as that can be in the JVM) and not the contents of the objects.

You should override .equals() method in order to compare every significant field of the objects. If your objects need this, they need an override too of the .hashCode() method according to Josh Bloch in Effective Java (or your objects would behave poorly when hashed into a Map)

Only if you guaranteed that your objects are immutable and that you don't create new instances for objects with the same value (say Boolean objects generated with Boolean.valueOf(true/false)) you could use that == ; in any other case, you need a .equals() method (or make them implement Comparable and try foo.compareTo(bar) == 0)

Jorge_B
  • 9,712
  • 2
  • 17
  • 22
-1

Because they are different objects.

Try changing a field/member in one object, and see if the field in the other object is changed as well.

GvS
  • 52,015
  • 16
  • 101
  • 139