• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object.equals() and == Operator  RSS feed

 
Terry Tucker
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String foo = "This is a foo!";
String bar = "This is a foo!";

// meaningfully equivalent...
System.out.println("foo.equals(bar) ? " + foo.equals(bar));

// are these not different objects?
System.out.println("foo == bar ? " + (foo == bar));

Both of these print statements return true. I would expect (foo == bar) to return false. Why is this?

Thanks...
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== in essence means same memory location. .equals() means content is equal. If two objects are the same Java stores them in the same place therefore == is true unless one of them has been created programatically . It's worth playing around with to see what happens. Also, == works differently with primitives than it does with objects.

Integer a = 1
Integer b = 1

a == b is true

// makes two different memory locations
Integer a = new Integer(1)
Integer b = new Integer(1)

a == b is false

~~~~~~~~~~~~~~~~~~

String zc = "c";
String za = "abc";
String zb = "ab"+zc;

za == zb is false


String za = "abc";
String zb = "ab"+ "c";

za == zb is true


~~~~~~~~~~~~~~~~~~~~~~~
short s = 1;
int i = 1;

s == i is true
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings work a little different, due to the String Pool. Any String that is a compile time constant is put into the pool, and all references point to the same one. You can try this to see == not work (NOT recommended for real code, just for an example):


(I think have that right...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, as you will see with the earlier example, Integers work differently too.

Start with this old JavaRanch Journal article.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another example:

The above gives you false on line 6 because the compiler cannot determine that the value of bar is going to be "Foo"; all it knows is that the value will be the concatenation of whatever the variable f and the variable oo happen to be when line 4 is executed. However, if you tell the compiler that the variables will never change, then it can determine the value assigned to bar and will pool the String values. Note that you don't need to declare bar as final in this case to get the Strings pooled.

 
Terry Tucker
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for you answers. I have been out of town for a couple days and am just now reading all of this.

Thanks for giving me understanding and for helping a novice
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!