Differences in Object modifications

后端 未结 3 414
我寻月下人不归
我寻月下人不归 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:19

    @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);
    

提交回复
热议问题