-5

I am confused with this example :

StringBuilder a = new StringBuilder("abcde");
String subStr = a.substring( a.indexOf("a") , a.indexOf("c")  );
int leng = a.length();
char ch = a.charAt(4);
System.out.println( subStr + " " + leng + " " + ch);
//the result will be : subStr = abc  , leng = 5  ch = e

My question is : Why ch = e and it doesn't create an Exception ?

My thinking:
I have one StringBuilder, a non immutable object and if I use a method on the object it will return me a new value of the object with the same reference object.

  • Why when I am using a.substring ( int a, int b ), it is not modifying the object StringBuilder ?
  • Why if I use the method a.append("value") I am modifying the value of the StringBuilder object?
Atri
  • 5,511
  • 5
  • 30
  • 40
lirio oliro
  • 21
  • 1
  • 7
  • Your question is confusing -- please try to clarify. – Hovercraft Full Of Eels Dec 27 '15 at 17:08
  • 1
    Calling `subString(...)` is a non-mutating method call. It simply returns a segment of the String or StringBuilder. Please edit your question itself for clarity. Specify exactly what in the API confuses you. – Hovercraft Full Of Eels Dec 27 '15 at 17:09
  • [Returns a **new** String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end - 1.](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#substring(int,%20int)) – WalterM Dec 27 '15 at 17:11
  • 2
    @HovercraftFullOfEels Just to clarify. Since Java 7, no-more the segment of the string is returned. Earlier it was the case causing memory leak and was a bug which was fixed. Please find more details [here](http://javarevisited.blogspot.in/2011/10/how-substring-in-java-works.html) and [here](http://stackoverflow.com/questions/15612157/substring-method-in-string-class-causes-memory-leak) – Aaditya Gavandalkar Dec 27 '15 at 17:55
  • @AadityaGavandalkar: many thanks for this information. I had not previously known about this interesting aspect. – Hovercraft Full Of Eels Dec 27 '15 at 18:49

4 Answers4

5

The .substring() method is returning a new String object, it does not modify the StringBuilder itself.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • I think that's it. The line is `char c = a.charAt(4);` That's the original "abcde" StringBuilder, not `subStr`. – markspace Dec 27 '15 at 17:11
  • I think the OP was confused about why the `.substring()` method is not modifying the `StringBuilder` contents, as he was expecting a different output for the `ch` character. – Mohammed Aouf Zouag Dec 27 '15 at 17:12
0

Why when i am using a.substring ( int a, int b ) it is not modifying the object StringBuilder ?

because substring() returns a new string ,it doesn't modify StringBuilder.

char ch = a.charAt(4); , charAt() returns a char based on the index and since index is zero base ,it returns e.

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
0

Just because an object is 'mutable' does not mean every method that object can perform has to modify it. Common examples are equals(), getClass() ... append() however does modify the StringBuilder object.

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
0

I have one StringBuilder, a non immutable object and if I use a method on the object it will return me a new value of the object with the same reference object.

  • Not always will a method return you the same reference object. It might create a new object and return that. Its always better to check the docs before you assume anything.

Why when I am using a.substring ( int a, int b ), it is not modifying the object StringBuilder?

  • If you read the documentation, it clearly states that substring(int,int) method Returns a new String. That answers why your object is not being modified.

Why if I use the method a.append("value") I am modifying the value of the StringBuilder object?

  • Again if you read the docs for append(String), it clearly states that it - Returns: a reference to this object.. This method complies with what "your thinking" was.
Atri
  • 5,511
  • 5
  • 30
  • 40