P.Rajesh

Greenhorn
+ Follow
since Apr 21, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by P.Rajesh

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");