String vs. StringBuilder

后端 未结 24 1511
眼角桃花
眼角桃花 2020-11-22 06:22

I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference

24条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:49

    String and StringBuilder are actually both immutable, the StringBuilder has built in buffers which allow its size to be managed more efficiently. When the StringBuilder needs to resize is when it is re-allocated on the heap. By default it is sized to 16 characters, you can set this in the constructor.

    eg.

    StringBuilder sb = new StringBuilder(50);

提交回复
热议问题