Strings and things
Written by Antoni Boucher   
Article Index
Strings and things
Solution
Solution

Many Java programmers use the == operator to compare strings.

Nevertheless, if you want to compare the value of two String objects, you need to use a method.

The equals() method compares the text of the two String objects.

If the text is the same, it will return true.

Otherwise, it returns false.

So, this snippet works as expected:

public class StringEquality {
 public static void main(String[] args) {
  String string1 = "abc", string2 = "def";
  if((string1 + string2).equals("abcdef")) {
   System.out.println("Equal");
  }
 }
}

But why does the == operator work sometimes and at other times not?

It is because String objects in Java are immutable and the == operator is comparing object references, not the text of the strings. These two facts work together to make it look as if the == is sometimes correctly comparing string values.

Therefore, if you create two strings using this syntax:

String string1 = "abc";
String string2 = "abc";

Java will not create two String objects with the same value - just a single object containing "abc".

So, string1 and string2 will be referencing the same object in memory.

This is the reason why:

string1 == string2

is true.

The == operator checks if both objects reference the same object in memory and in this case this also means that the two strings are equal in value.

However if you use the String constructor, a new space in memory will be used for the new String object. That is following:

String string1 = "abc";
String string2 = new String("abc");

We have two string objects both holding the same text but now:

string1 == string2

is false because the two variables reference different object. The same thing happens if you compute a result on the fly or compare a string to a literal. For example:

string1 + string2 == "abcdef"

is false for two reasons. First string1 and string2 are concatenated as a new object and "abcdef" is yet another string object, and so they cannot be equal as references to an object.

Pattern

You should always use the equals() method if you want to compare the text of two String objects.

You might think that it is a disadvantage to be unable to compare String objects with the == operator.

But, don't forget that String in Java is not a primitive type; it is a class and hence uses reference semantics not value semantics.

Thus, we use == to know if two string objects are actually the same object in memory which also, almost as a side effect, ensures that they have the same text.

 

Banner

More Puzzles

Sharpen Your Coding Skills
Self-Descriptive Arrays

Put on your thinking cap for another set of conundrums that will exercise your coding skills. This time Melvin Frammis introduces his junior partner Bugsy Cottman to some classic number puzzles that c [ ... ]


Sharpen Your Coding Skills
Three Warehouses Puzzle

Talking through a problem is often a good way to see what is required for its solution. Reducing it in scale is another good strategy. But as programmers at the end of the day we need to code an algor [ ... ]


Sharpen Your Coding Skills
Towers Of Hanoi Mutants

Towers of Hanoi is a classic puzzle and is often used to illustrate the idea of recursion. Here Melvin Frammis challenges you to attempt to find solutions to some variations - but first he explains th [ ... ]


Other Articles

 

<ASIN:0132130807>

<ASIN:0672330768>
<ASIN:0136091814>

<ASIN:0321356683>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.