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?