StringBuilder is not thread safe. So, it performs better in situations where thread safety is not required.

StringBuffer is implemented by using synchronized keyword on all methods.

 

StringBuffer

/*      */   public synchronized StringBuffer append(String string)
/*      */   {
/*  197 */     if (string == null) string = String.valueOf(string);
/*  198 */     int adding = string.length();
/*  199 */     int newSize = this.count + adding;
/*  200 */     if (newSize > this.value.length) {
/*  201 */       ensureCapacityImpl(newSize);
/*      */     }
/*  203 */     string.getChars(0, adding, this.value, this.count);
/*  204 */     this.count = newSize;
/*  205 */     return this;
/*      */   }

StringBuilder

/*      */   public StringBuilder append(String string)
/*      */   {
/*  201 */     if (string == null) string = String.valueOf(string);
/*  202 */     int adding = string.length();
/*  203 */     int newSize = this.count + adding;
/*  204 */     if (newSize > this.value.length) {
/*  205 */       ensureCapacityImpl(newSize);
/*      */     }
/*  207 */     string.getChars(0, adding, this.value, this.count);
/*  208 */     this.count = newSize;
/*  209 */     return this;
/*      */   }

 

相关文章:

  • 2018-10-11
  • 2021-07-15
  • 2021-06-18
  • 2022-02-04
  • 2021-12-15
  • 2022-01-27
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-09-28
  • 2022-01-09
  • 2021-10-22
  • 2021-07-26
  • 2021-07-14
  • 2021-10-22
相关资源
相似解决方案