Differences in Object modifications

后端 未结 3 464
我寻月下人不归
我寻月下人不归 2021-01-22 15:59

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

    StringBuilder s=new StringBuilder(\"0123456789\");
    s.substring(1, 2);
    System.out.printl         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 16:17

    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.

提交回复
热议问题