Languages
[Edit]
EN

Java - compare strings

3 points
Created by:
Yusef-Ewing
479

One of the most common problem that affects new Java Developers is how to compare two String objects. They try to compare strings with == operator what makes only reference comparision - not values. To solve this problem equals method should be used. This article explains how to compare two strings.

1. String.equals method example

Edit

Note: this approach is available sice java 1.7.

Output:

2. Object.equals method example

Edit

Output:

3. How to do not compare strings in Java

Edit

Output:

Note: for all "text 1" occurrences java uses same object in memory what caused true result during references comparison with == operator.

Alternative titles

  1. Java - how to compare strings?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String (popular problems)

Java - compare strings
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join