Home java How to compare strings in java?

How to compare strings in java?

Author

Date

Category

In your program, I used the == statement for comparing rows. But I came across the bug, and when replacing == on Equals he disappeared.

You should avoid the operator == ? When you can use it, and when not? What is the difference?


Answer 1, Authority 100%

Operator == compares links.

Equals method compares values.

Consequently, if you want to compare strings on equality, you should use Equals .

However, in some cases, the strings are guaranteed to be the same as the same object due to Pula strings (string interning ). These cases are explicitly described in Java language specification .

The == statement is used to verify that two lines indicate the same object.

// these lines have the same value
New String ("Test"). Equals ("Test") // - & gt; True.
// ... but these are different objects
New String ("Test") == "Test" // - & gt; false
// ... these lines are also different objects
New String ("Test") == New String ("Test") // - & gt; false
// ... But these lines indicate the same object,
// Because the compiler adds all literal literals to the pool.
"Test" == "Test" // - & gt; True.
// Concatenation of literals also occurs at the compilation stage,
// so they point to one object
"Test" == "TE" + "ST" // - & gt; True.
// But the SUBSTRING () call occurs during execution,
// As a result, different objects are obtained.
"Test" == "! Test" .Substring (1) // - & gt; false
// Lines from the pool can be obtained by calling Intern ().
"Test" == "! Test" .Substring (1) .intern () // - & gt; True.

It should be noted that == is noticeably faster than Equals (comparison of the reference instead of calling the method and the incentive comparison, if the rows of different lengths), so if you work with Lines from the pool (or systemic, or one), replacing the Equals on == can lead to a noticeable acceleration. But this happens very rarely .

Beware of the call Equals on NULL ! Operator == Perfectly compares strings if one or more of them is NULL , but calling the Equals method on a row equal to null will excel.

To compare strings that can be equal to 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 use modern development environments, they will warn if you try to compare strings using the == statement. Always pay attention to such warnings.


Answer 2, Authority 8%

If you are short, then == compares the references to the object if the links indicate the same object, then it is tru, otherwise false, in the case of primitive types == compares values.

Equals () Used in String So, he takes and compares preventingly each STRING , but it is only with String if you take the rest of the objects (you created the objects Apple and Pear ), while in these classes is not registered Equals method, then it’s like and == compares references to the object, if it is the same object, then labor otherwise false.

String Registered Equals () method, which compares prevail, so with String you need to use Equals ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions