StringBuilder vs String concatenation in toString() in Java

后端 未结 18 2198
北恋
北恋 2020-11-21 04:18

Given the 2 toString() implementations below, which one is preferred:

public String toString(){
    return \"{a:\"+ a + \", b:\" + b + \", c: \"         


        
18条回答
  •  天涯浪人
    2020-11-21 05:05

    I also had clash with my boss on the fact whether to use append or +.As they are using Append(I still cant figure out as they say every time a new object is created). So I thought to do some R&D.Although I love Michael Borgwardt explaination but just wanted to show an explanation if somebody will really need to know in future.

    /**
     *
     * @author Perilbrain
     */
    public class Appc {
        public Appc() {
            String x = "no name";
            x += "I have Added a name" + "We May need few more names" + Appc.this;
            x.concat(x);
            // x+=x.toString(); --It creates new StringBuilder object before concatenation so avoid if possible
            //System.out.println(x);
        }
    
        public void Sb() {
            StringBuilder sbb = new StringBuilder("no name");
            sbb.append("I have Added a name");
            sbb.append("We May need few more names");
            sbb.append(Appc.this);
            sbb.append(sbb.toString());
            // System.out.println(sbb.toString());
        }
    }
    

    and disassembly of above class comes out as

     .method public ()V //public Appc()
      .limit stack 2
      .limit locals 2
    met001_begin:                                  ; DATA XREF: met001_slot000i
      .line 12
        aload_0 ; met001_slot000
        invokespecial java/lang/Object.()V
      .line 13
        ldc "no name"
        astore_1 ; met001_slot001
      .line 14
    
    met001_7:                                      ; DATA XREF: met001_slot001i
        new java/lang/StringBuilder //1st object of SB
        dup
        invokespecial java/lang/StringBuilder.()V
        aload_1 ; met001_slot001
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lan\
    g/StringBuilder;
        ldc "I have Added a nameWe May need few more names"
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lan\
    g/StringBuilder;
        aload_0 ; met001_slot000
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/Object;)Ljava/lan\
    g/StringBuilder;
        invokevirtual java/lang/StringBuilder.toString()Ljava/lang/String;
        astore_1 ; met001_slot001
      .line 15
        aload_1 ; met001_slot001
        aload_1 ; met001_slot001
        invokevirtual java/lang/String.concat(Ljava/lang/String;)Ljava/lang/Strin\
    g;
        pop
      .line 18
        return //no more SB created
    met001_end:                                    ; DATA XREF: met001_slot000i ...
    
    ; ===========================================================================
    
    ;met001_slot000                                ; DATA XREF: r ...
        .var 0 is this LAppc; from met001_begin to met001_end
    ;met001_slot001                                ; DATA XREF: +6w ...
        .var 1 is x Ljava/lang/String; from met001_7 to met001_end
      .end method
    ;44-1=44
    ; ---------------------------------------------------------------------------
    
    
    ; Segment type: Pure code
      .method public Sb()V //public void Sb
      .limit stack 3
      .limit locals 2
    met002_begin:                                  ; DATA XREF: met002_slot000i
      .line 21
        new java/lang/StringBuilder
        dup
        ldc "no name"
        invokespecial java/lang/StringBuilder.(Ljava/lang/String;)V
        astore_1 ; met002_slot001
      .line 22
    
    met002_10:                                     ; DATA XREF: met002_slot001i
        aload_1 ; met002_slot001
        ldc "I have Added a name"
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lan\
    g/StringBuilder;
        pop
      .line 23
        aload_1 ; met002_slot001
        ldc "We May need few more names"
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lan\
    g/StringBuilder;
        pop
      .line 24
        aload_1 ; met002_slot001
        aload_0 ; met002_slot000
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/Object;)Ljava/lan\
    g/StringBuilder;
        pop
      .line 25
        aload_1 ; met002_slot001
        aload_1 ; met002_slot001
        invokevirtual java/lang/StringBuilder.toString()Ljava/lang/String;
        invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lan\
    g/StringBuilder;
        pop
      .line 28
        return
    met002_end:                                    ; DATA XREF: met002_slot000i ...
    
    
    ;met002_slot000                                ; DATA XREF: Sb+25r
        .var 0 is this LAppc; from met002_begin to met002_end
    ;met002_slot001                                ; DATA XREF: Sb+9w ...
        .var 1 is sbb Ljava/lang/StringBuilder; from met002_10 to met002_end
      .end method
    ;96-49=48
    ; ---------------------------------------------------------------------------
    

    From the above two codes you can see Michael is right.In each case only one SB object is created.

提交回复
热议问题