You need to understand simple concept, when a new object will be created in context to String..Correct.
1- everytime a contructor is used by using new, it is true for any object of Java.
2- even a string if created via String pool, and when it doesn't exists already in pool.Meaningless. To the extent I can make sense of this, it is incorrect.
So, there will be two objects in your example...There will indeed, but not for the reasons you have given.
1- String S= "abc"; first occurence of your codebase line, assuming "abc" already doesn't exists in pool.But "abc" does exist in the string pool, by definition. All string literals are pooled. This line does not create a new object. It creates a reference to an existing object in the string pool.
2- String S1= new String ("abc"); //This will always create new object doesn't matter if exists or not in the pool.Correct except that in this case once again "abc" does exist in the pool.
And remaning two lines wouldn't impact the object creation, that is just a reference assignment for string pool and created object.Of the remaining two lines, the first is the same as the original first line, so it is a new reference to an existing object, and the second is also a new reference to an existing object.
hope, this helps you.It won't because it is 50% incorrect.
EJP wrote:Not fair. Back then schools actually trained useful people. Nowadays even math is going out the door because it is "too hard".
1971. My first two assignments were a bubble sort and a heap sort, the latter to prove that the former is simple but useless. Never used a bubble sort since. Very useful bit of knowledge.