Remember that hashcode is not guaranteed to be different for unequal objects, also two different String instances with the same value will return the same hashcode. Also, even though there is no reference to the String literal (Object) "Pal", it is still an object and will be added to the String pool (assuming "Pal" doesn't already reside in the string pool).
Kristian is right about hashcode theory. In Java, the hashcode produced for any string is calculated by adding the hashvalues for all characters. Hence in this case the hashcodes for s1&s2 and s3&s4 match. But why should "Pal" create a new object?
No - that's because in fact, s1 and s2 refer to the same object. And s3 and s4 refer to the same object. Because the code explicitly assigned s2 = s1, and s4 = s3. It's not just some coincidence.
I don't think Jim Yingst is right, when I create a string object String s = "Hello", It won't create a new object in the pool for me if there is already a "Hello" in the pool, it will just set the reference of my variable s to the exisiting "Hello".
I believe String objects are not garbage collected like normal Objects. They still exist in the pool even if there is no variable referencing it.
Jim is right, but he doesn't qualify his statement enough. For String (which is what we're talking about here) he is right. For some other class he may not be (as each class can and often will have a distinct hascode algorithm relevant to the data contained within an instance of that class).
You are correct in assuming that garbage collection for Strings in the String constant pool is different from garbage collection of any object anywhere else, but that's not relevant in this scenario as the question was how many objects are created, not how many exist at the end of the code block (the answer to that could be undefined even).
In this scenario, at most 3 objects are created on the string constant pool. A total of 4 references are created, 2 each to 1 of these objects. The last of them ("Pal") has no reference at all but does exist and would be reused if another assignment 's5 = "Pal";' were added. I had to qualify my statement with "at most" because as Kristian notes any one or more of the strings may already have existed in the string constant pool and therefore might not have been created at all, but during the exam you can (almost, it will be explicitly mentioned if not) assume that such situations will not occur (in fact, because of tricks like this you're highly unlikely to see questions like this using String as a datatype on the real exam).
Sujit, my statements said nothing at all about the string pool, or garbage collection. I don't see what your reply has to do with anything I actually said.
Jeroen, I was making a direct reply to Abhishek, who clearly said he was talking about Strings. I quoted him. The fact that I referred to "characters" would be another clue. Yes, we're talking about Strings. In what way was that not clear?
"I'm not back." - Bill Harding, Twister
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss