class Test
{
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer("iswasif");
StringBuffer sb2 = sb1;
StringBuffer sb3 = new StringBuffer("iswasif");
if(sb1==sb2) {
System.out.println("sb1 and sb2 are equal"); //References are same
}
if(sb2==sb3) {
System.out.println("sb2 and sb3 are equal"); //References are different
}
}
}
Answer is 2 objects.
If you look at the above example, you will come to know that how many objects were created in the heap.
StringBuffer sb2 = sb1;
This statement says that sb2 refer sb1, whatever object sb1 pointing the same object sb2 also pointing. That means, sb1 and sb2 are references which pointing the same objects.
Second object is created because of the below statement
StringBuffer sb3 = new StringBuffer("iswasif");