Java中String、StringBuilder和StringBuffer

 

 

And internal StringBuilder StringBuffer are achieved by char []. (After jdk1.9, the underlying char array into a byte [].) The only difference is that our internal operations StringBuffer methods are combined with the synchronized keyword, because to ensure the security thread, and the efficiency is low compared to the StringBuilder.

String immutable benefits:
  1. Safety, as our String is immutable, born with a thread-safe.
  2.String often as a parameter, String immutable, on behalf of our parameters are not changed.
  3. The constant need to pool our String in our development, which often are used, our constant pool will be cached String, String only immutable, constant pool meaningful again. We save memory space.
  4. When we HashMap String as a hash table, or other key, as immutable String, so the hash value is not changed, and we do not need to calculate each may cache their hash value, can be improved Hash table s efficiency.

Guess you like

Origin www.cnblogs.com/roadlandscape/p/12082023.html