Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Object.equals() and == Operator

 
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 12989
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 72974
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 16267
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72974
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic