Hey Moieen Khatri,
The number of objects created here is 5.To find the number of unique objects created you can use the following in your code.
class
test {
public static void main(String args[])
{
System.out.println(makinStrings());
}
public static String makinStrings() {
String s = "Fred";
System.out.println(s+" "+s.hashCode());
s = s + "47";
System.out.println(s+" "+s.hashCode());
s = s.substring(2, 5);
System.out.println(s+" "+s.hashCode());
s = s.toUpperCase();
System.out.println(s+" "+s.hashCode());
s=s.toString();
System.out.println(s+" "+s.hashCode());
return s;
}
}
I have just a call to the hashCode() method every time I make a change to the String Object s.When I run the above code , I get the following output.
bash-3.00#
java test
Fred 2198155
Fred47 2112428622
ed4 100213
ED4 68469
ED4 68469
ED4
This shows there are 5 unique String Objects created since the hashCode() method returns different values each time it is called.
Hope this Helps.
regards,
Raja