Gareth D.
—How do I compare strings in Java?
To compare strings in Java for equality, you should use String.equals()
.
public class Main { public static void main(String[] arg) { String str1 = "java"; String str2 = "java"; System.out.println(str1.equals(str2)); } }
Output:
true
If uppercase and lowercase difference isn’t important, you can use String.equalsIgnoreCase()
.
public class Main { public static void main(String[] arg) { String str1 = "java"; String str2 = "Java"; System.out.println(str1.equalsIgnoreCase(str2)); } }
Output:
true
If you want to find out if a String is “bigger” or “smaller” than another string (that is, whether it comes before or after alphabetically), use String.compareTo()
.
public class Main { public static void main(String[] arg) { String str1 = "aaa"; String str2 = "bbb"; String str3 = "ccc"; String str4 = "bbb"; System.out.println(str2.compareTo(str3)); System.out.println(str2.compareTo(str1)); System.out.println(str2.compareTo(str4)); } }
Output:
-1 1 0
The first output is -1
, indicating that str2
("bbb"
) comes before str3
("ccc"
).
The second output is 1
, indicating that str2
("bbb"
) comes after str1
("aaa"
).
The third output is 0
, indicating that str2
("bbb"
) is equal to str4
("bbb"
).
==
for string comparisonStrings in Java are stored on the heap, and the variable only stores a reference to where the real string value can be found.
If you use ==
to compare strings, you might get unexpected results.
public class Main { public static void main(String[] arg) { String str1 = new String("java"); String str2 = new String("java"); System.out.println(str1 == str2); } }
Output:
false
Here, the two strings have equal values but these values are not stored in the variables str1
and str2
.
When dealing with primitive types like int
, any variables with the same value will also have the same identity code. But when dealing with non-primitive types like String, this is not the case.
public class Main { public static void main(String[] arg) { String str1 = new String("java"); String str2 = new String("java"); int int1 = 1; int int2 = 1; System.out.println(System.identityHashCode(int1)); System.out.println(System.identityHashCode(int2)); System.out.println(System.identityHashCode(str1)); System.out.println(System.identityHashCode(str2)); } }
Output:
808993268 808993268 1229416514 2016447921
What makes this confusing is that often ==
will return true
if you compare two strings with the same value. In the code above we used new String("java")
to create two separate strings, but if we create the strings without the explicit new
keyword, Java will do some optimization in the background and only store the string once.
public class Main { public static void main(String[] arg) { String str1 = "java"; String str2 = "java"; System.out.println(System.identityHashCode(str1)); System.out.println(System.identityHashCode(str2)); // do not do this System.out.println(str1 == str2); } }
Here, the two strings have the same hashcode, and ==
will tell you that they are the same string. This is a side effect of Java’s memory optimization and shouldn’t be relied on, as it may not always behave consistently.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.