I'm new to Java, and have a question related to the creation of strings.
Case 1:
String a = "hello";
String b = "world";
a = a + b;
System.out.println(a);
Case 2:
String a;
String a = "hello";
a = new String("world");
System.out.println(a);
I would like to know how many objects are created in each case. Because String is immutable so once value is assigned to it that object cannot be reused (that's what I understand currently, please correct me if I'm wrong).
And I would be even more happy if anyone can explain the same with StringBuffer. Thanks.