How is StringBuffer implementing append function without creating two objects?

后端 未结 10 1117
执念已碎
执念已碎 2021-01-30 09:22

It was an interview question. I was asked to implement the StringBuffer append function. I saw the code after the interview. But I cannot understand how the operati

10条回答
  •  旧时难觅i
    2021-01-30 09:58

    ****String s1="Azad"; ----One object will create in String cons. pool
    
    System.out.println(s1);--output--Azad
    
    s1=s1.concat("Raja");  Two object will create 1-Raja,2-AzadRaja and address of AzadRaja Store in reference s1 and cancel ref.of Azad object 
    
    System.out.println(s1);  --output AzadRaja****
    

提交回复
热议问题