Strings are immutable - that means I should never use += and only StringBuffer?

前端 未结 11 1438
醉梦人生
醉梦人生 2021-01-04 08:12

Strings are immutable, meaning, once they have been created they cannot be changed.

So, does this mean that it would take more memory if you append things with += th

11条回答
  •  醉梦人生
    2021-01-04 09:04

    Yes you would and that is exactly why you should use StringBuffer to concatenate alot of Strings.

    Also note that since Java 5 you should also prefer StringBuilder most of the time. It's just some sort of unsynchronized StringBuffer.

提交回复
热议问题