Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

compare objects

 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output: false
true
isn't supposed that the first output be 'true' since they are both the same object? however the second output is correct as expected.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isn't supposed that the first output be 'true' since they are both the same object?
They might be the same object type, but the reference variables points to different objects.
try this:

What's the result?
[ July 29, 2003: Message edited by: Andres Gonzalez ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


They might be the same object type, but the reference variables points to different objects.


if this is the case then System.out.println("s1.equals(s2)? " + s1.equals(s2)) should output false (which isn't the case)
please also note that the purpose of my question is to understand the different behaviour of the test and String objects
[ July 29, 2003: Message edited by: Vicken Karaoghlanian ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh.. i see.. sorry
The problem here is with the equals() method. The String class overrides the equals() method from Object, so when you do this:

Test is not overriding equals, so


The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).


http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Object.html#equals
This is the main reason it returns false.
Now, String class overrides equals, so when you do equals() it returns true.


String and wrapper classes override equals to check for values.


From KnB book.
I hope this helps. let me know
[ July 29, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vicken
As Andres pointed out the output is so because the Object's class equals() method compares for object equality by checking if the references to both the objects point to the same thing or not. But in the String class the String class overrides equals() method to provide the equals method with functionality that two strings would be considered equal if their contents are equal.
Code for the Object's equals() method :

Code for String's equals() method :
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing the StringBuffer class doesn't overrides the equals() method.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating 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
    Bookmark Topic Watch Topic
  • New Topic