My questions is regarding string pool in java.
Case 1:
StringBuilder sb = new StringBuilder();
sb.append("First");
sb.append("Two");
sb.append("Three");
sb.append("Four");
Case 2:
StringBuilder sb = new StringBuilder();
sb.append("First"+"Second"+"Three"+"Four");
How many string objects will be there in string pool after execution of each above 2 cases?(Note : Assume string pool have 0 object before each case.)
My assumptions=>
In 1st case:
String pool will have 4 string objects at the end of 1st case. How? Explanation: String "First" will get created, it will add in string pool & sb will be modified. Then another string object "Two" will get created, will keep it in string pool & sb will get modified. In same way, at the end of the first case, string pool will have 4 string objects.
In 2nd case: String pool will have 7 string objects. How? Explanation: String "First" & "Two" will get created in pool, then since we are concatenating "First" & "Two", third string object "FirstSecond" will get created in string pool. In the same way, at the end of the 2nd case, string pool will have 7 objects.
Correct me if I am wrong.