Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Demonstrating string comparison with Java

I want to demonstrate with a few line of code that in Java, that to compare two strings (String), you have to use equals() instead of the operator ==.

Here is something I tried :

public static void main(String Args[]) {
   String s1 = "Hello";
   String s2 = "Hello";

   if (s1 == s2)
      System.out.println("same strings");
   else
      System.out.println("different strings");
}

I was expecting this output : different strings, because with the test s1 == s2 I'm actually comparing two references (i.e. addresses) instead of the objet's content.

But I actually got this output : same strings !

Browsing the internet I found that some Java implementation will optimize the above code so that s1and s2 will actually reference the same string.

Well, how can I demonstrate the problem using the == operator when comparing Strings (or Objects) in Java ?

like image 244
Jérôme Avatar asked Aug 31 '10 19:08

Jérôme


People also ask

How do you compare strings in Java?

There are three ways to compare strings in Java. The Java equals() method compares two string objects, the equality operator == compares two strings, and the compareTo() method returns the number difference between two strings. String comparison is a crucial part of working with strings in Java.

Can we compare 2 strings using == in Java?

To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

Does == work for strings in Java?

The == operator does not work reliably with strings. Use == to compare primitive values such as int and char. Unfortunately, it's easy to accidentally use == to compare strings, but it will not work reliably. Remember: use equals() to compare strings.

Can we use == for string?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .


2 Answers

The compiler does some optimizations in your case so that s1 and s2 are really the same object. You can work around that by using

String s1 = new String( "Hello" );
String s2 = new String( "Hello" );

Then you have two distinct objects with the same text content.

like image 178
tangens Avatar answered Oct 13 '22 18:10

tangens


Well, how can I demonstrate the problem using the == operator when comparing Strings (or Objects) in Java ?

Here a way:

String s = "ab";
String t = new String("ab");
System.out.println(s == t); // false

Also be careful when comparing primitive wrappers and using auto-boxing: Integer (and Long) for instance caches (and re-uses!) the values -128..127. So:

Integer s = -128;
Integer t = -128;
System.out.println(s == t);

will print true, while:

Integer s = -129;
Integer t = -129;
System.out.println(s == t);

prints false!

like image 32
Bart Kiers Avatar answered Oct 13 '22 18:10

Bart Kiers