10

I have a little confusion regarding the difference between subSequence method and subString method in Java String class. I read the article What is the difference between String.subString() and String.subSequence() which was been answered to this question but I have a little confusion, the article mentioned "Its read only in the sense that you can't change the chars within the CharSequence without instantiating a new instance of a CharSequence".

But when I tried with the following example it

String string = "Hello";
CharSequence subSequence = string.subSequence(0, 5);
System.out.println(subSequence.subSequence(1, 4));
subSequence = subSequence.subSequence(1, 4);
System.out.println(subSequence);

it prints
ell
ell

I do not know whether I have understood it correctly. Can you please help me to clarify this and please tell me the difference between subString and subSequence with an example

thanks a lot

Dilan
  • 1,389
  • 5
  • 14
  • 24

3 Answers3

20

As explained in the article you linked to, the only difference between those two methods is the return type. One returns a String and the other returns a CharSequence.

The thing to understand is that CharSequence is an interface, and String is a class which implements CharSequence.

So every String is also a CharSequence, but not vice versa. You can assign the output of substring() to a variable of type CharSequence, but not the other way around:

String string = "Hello";

String subString1 = string.substring(1,4);             // ell
String subString2 = string.subSequence(1, 4);          // Type mismatch compiler error

CharSequence subSequence1 = string.subSequence(1, 4);  // ell
CharSequence subSequence2 = string.substring(1, 4);    // ell

In this particular case, since we're executing the subSequence() method on a String object, the String implementation will be invoked, which just returns a substring():

public CharSequence subSequence(int beginIndex, int endIndex) {
    return this.substring(beginIndex, endIndex);
}

This is what people are talking about when they say the substring() and subSequence() methods are identical when you call them on a String.


Regarding the code you posted, since you started with a String, all your calls to subSequence() are really just substring() calls under the covers. So each time you try to "change" your CharSequence, the compiler is really just creating another String object and passing you a reference to the new one, rather than changing the existing object.

Even though your variable is of type CharSequence, the object it refers to is really a String.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • `subSequence` could even return a `StringBuilder` which is also a `CharSequence` –  Oct 25 '14 at 08:20
  • @a_horse_with_no_name Actually, the implementation of subSequence() in the StringBuilder and StringBuffer classes also both return a String. I'm not sure about other implementations, though. – azurefrog Oct 25 '14 at 08:30
0

"Its read only in the sense that you can't change the chars within the CharSequence without instantiating a new instance of a CharSequence".

I believe that part of your confusion is that you think you are changing the chars within the CharSequence with the following code:

CharSequence subSequence = string.subSequence(0, 5);
subSequence = subSequence.subSequence(1, 4);

Note that the second line creates a brand new CharSequence object and overwrites the original one. It does not change the original CharSequence. To understand this better, I suggest you read a little more about reference variables.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
-1

Hint: Prepare two examples, one using subSequence and one using subString. Each will give a result. Try to modify both results and watch what happens.

A modification is any operation on the string, which would result in a different string, like adding or removing characters, or changing characters.

mvw
  • 5,075
  • 1
  • 28
  • 34