Using equals () method with String and Object in Java

Object o1 = new Object(); Object o2 = new Object(); //o1=o2; System.out.println(o1.equals(o2)); 

Returns false . It can return true if the comment is deleted.


Why does this not apply to the String class?

 String s1=new String(); String s2=new String(); System.out.println(s1.equals(s2)); 

It returns true . What for? (because String uses interns or something else?)

+7
source share
6 answers

Because equals () for String compares the content, not the object itself.

public boolean equals (Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

  /* String.equals() */ public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } return true; } } return false; } 

(Link to String.equals () source)

Compared to equals for an object :

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

 /* Object.equals() */ public boolean equals(Object obj) { return (this == obj); } 

(Link to the source of Object.equals () )

Also, don't forget the equals() function contract:

The equals method implements an equivalence relation for non-zero object references:

  • This is reflective : for any non-zero reference value x , x.equals(x) should return true.
  • symmetric : for any non-zero reference values x and y , x.equals(y) should return true if and only if y.equals(x) returns true.
  • Transitive : for any non-zero reference values x , y and z , if x.equals(y) returns true and y.equals(z) returns true , then x.equals(z) should return true .
  • agreed : for any non-zero reference values x and y several calls to x.equals(y) successively return true or successively return false if there is no information used for equal comparisons with objects.
  • For any non-zero reference value x , x.equals(null) should return false .

It is also recommended to read:

+17
source

equals for Object compares memory references.
This is why this is not true, since they are different Object s
equals for String overridden for character-based comparisons.
You have 2 empty String objects, so equals returns true .

+8
source

== compares the addresses of objects / lines / nothing

.equals() , designed to use the internal state of objects for comparison.

So:

new Object() == new Object() => false - two separate objects at different addresses in memory.

new String("a") == new String("a") => false - the same situation - two separate addresses for string objects.

new String("a").equals(new String("a")) => true - the addresses are different, but Java accepts one state of the object ('a') and compares it with another state of the object ('a'), it will consider their equal and will report the truth.

Using the equals () method, you can encode the comparison in any way suitable for your program.

intern() is a slightly different story. It is intended to return the same object (address) for the same char sequence. It is useful to reduce the amount of memory required when creating multiple lines.

new String("aaa").intern() will look in the computer's memory if someone created the string "aaa" before and returns the first instance of String ... If it was not found, the current one will be credited as the first and on and on "aaa ".intern () and new String("aaa").intern() and ("a"+"aa").intern() will return this" first "instance.

Beware: "aaa".intern() does not work very fast, and if you put all the lines, you will save some memory, but lose a lot of work with the processor.

+4
source

equals implemented in the Object class compares only references. Here is the source code:

 public boolean equals(Object obj) { return (this == obj); } 
+2
source

The equals method must be redefined inside the class if you want to make it behave in some other way. By default, it checks to see if two links are referencing the same object.

+2
source

The equals() method of the Object class does not know how to compare strings; it knows how to compare objects. To compare strings, a string class overrides the equals() method and compares the strings in it.

Object.equals() will only compare references, where String.equals() will compare the values.

+1
source

All Articles