In my program, I used the == operator to compare strings. But I came across a bug, and when replacing == with equals it disappeared.
Should operator avoid == ? When can it be used and when not? What is the difference?
In my program, I used the == operator to compare strings. But I came across a bug, and when replacing == with equals it disappeared.
Should operator avoid == ? When can it be used and when not? What is the difference?
Operator == compares links.
The equals method compares values.
Therefore, if you want to compare strings for equality, you should use equals .
However, in some cases, strings are guaranteed to be represented by the same object due to string interning . These cases are explicitly described in the Java language specification .
The == operator is used to verify that two lines point to the same object.
// Эти строки имеют одно и тоже же значение new String("test").equals("test") // --> true // ...но это разные объекты new String("test") == "test" // --> false // ...эти строки тоже разные объекты new String("test") == new String("test") // --> false // ...но эти строки указывают на один и тот же объект, // потому что компилятор добавляет все литералы в пул. "test" == "test" // --> true // Конкатенация литералов тоже происходит на стадии компиляции, // поэтому они указывают на один объект "test" == "te" + "st" // --> true // но вызов substring() происходит во время выполнения, // в результате получаются разные объекты. "test" == "!test".substring(1) // --> false // Строки из пула могут быть получены с помощью вызова intern(). "test" == "!test".substring(1).intern() // --> true It should be noted that == noticeably faster than equals (comparing a link instead of calling a method and character-by-character comparison if the strings are of different lengths), so if you work with strings from the pool (or system, or your own), replacing equals with == can lead to a noticeable acceleration. But this happens very rarely .
Beware of calling equals to null ! The == operator perfectly compares strings if one or more of them is null , but a call to the equals method on a string equal to null will result in an exception.
To compare strings that can be null , you can use the following method:
public static boolean equals(String str1, String str2) { return str1 == null ? str2 == null : str1.equals(str2); } It is present in some third-party libraries, for example, in Apache Commons.
If you are using modern development environments, they will warn you if you try to compare strings using the == operator. Always pay attention to such warnings.
In short, == compares object references, if links point to the same object, then this is true, otherwise false, in the case of primitive types == compares values.
equals () used in String so it takes and compares each String character by character, but this is only with String , if you take the remaining objects (you created the Яблоко and Груша objects), and the equals method is not registered in these classes, then it is == compares links to an object, if it is the same object, then I rub false otherwise.
In the String the equals () method is registered, which compares character-by-character, so you should use equals () with String
Source: https://ru.stackoverflow.com/questions/757923/
All Articles