StringBuffer sb = new StringBuffer("abc");
StringBuffer sb1 = sb;
StringBuffer sb2 = new StringBuffer("abc");
How many objects are created?
StringBuffer objects are created because there is 2 new.String object is created [JLS, 3.10.5 => It is guaranted that the object will be reused by any other code running in the same virtual machine that happens to contain the same string literal]String objects?char[], which is an Object, etc.StringBuffer objects, each of which will contain a char[]String object which will contain a char[]So 6 objects in total.
If any code referencing "abc" has previously been run, then the String won't be created, so only 4 objects will be created.
You could start the debugger and simply count how many times your run over a new ... Advanced: Use https://hat.dev.java.net/ or http://www.eclipse.org/mat/ or other Heap tools.
StringBuildervsStringBufferaspect...