Differences in Object modifications

时间秒杀一切 提交于 2019-12-02 02:02:53

问题


i was just wondering if anybody could help me out with this :

    StringBuilder s=new StringBuilder("0123456789");
    s.substring(1, 2);
    System.out.println(s);
    s.delete(2, 8);
    System.out.println(s);

the first Sysout gives 0123456789(although i expected a substring) but other Sysout gives 0189. I have noticed that also with some Time and Date classes.How can i figure out, when what form is going to modify original object (in this case s). Is this related to Mutability of objects? Is there any general rule? Thanks in advance HK


回答1:


If you see the substring method definition in AbstractStringBuilder abstract class which later extended by StringBuilder class, you will find below code:

public String substring(int start, int end) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (end > count)
        throw new StringIndexOutOfBoundsException(end);
    if (start > end)
        throw new StringIndexOutOfBoundsException(end - start);
    return new String(value, start, end - start);
}

From the method definition you can see that it is returning a new String object, the method is not working on actual StringBuilder content. So their will no change in the content of StringBuilder object but rather a new String object will be returned.

Now if you see delete method definition inside StringBuilder class it is:

@Override
public StringBuilder delete(int start, int end) {
    super.delete(start, end);
    return this;
}

And the definition of delete in AbstractStringBuilder (StringBuilder super class) is :

public AbstractStringBuilder delete(int start, int end) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (end > count)
        end = count;
    if (start > end)
        throw new StringIndexOutOfBoundsException();
    int len = end - start;
    if (len > 0) {
        System.arraycopy(value, start+len, value, start, count-end);
        count -= len;
    }
    return this;
}

From the method definition it could be clearly understood that it is working on same StringBuilder object content and it is not returning a new object but rather the same object reference which is passed to it.




回答2:


The Javadoc tells you whether a method modifies the instance it operates on.

substring

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.

delete

Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.

Hence substring doesn't change the state of the StringBuilder while delete does.




回答3:


@Hauptman Koening

Try this with your own example, hope it will clarify

    StringBuilder s = new StringBuilder("0123456789");
    String substring = s.substring(1, 2); // See here it returns a String, remember Strings are constants i.e. not mutable, not modifying the original StringBuilder s
    System.out.println(substring);
    StringBuilder delete = s.delete(2, 8); // see here it returns the String Builder, so remember StringBuilder is a mutable sequence of characters, hence modified the original
    System.out.println(delete);


来源:https://stackoverflow.com/questions/41423324/differences-in-object-modifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!