I attended interview and i was asked this question.
String s=new String("Rohit");
Does this statement creates an object in heap only or it makes an entry in string pool as well?
I answered it does not make entry in pool. I think with .intern()
it would make entry in string pool. Interviewer's thought was opposite.
Could you please guide me if i was wrong or interviewer?
Thanks in Advance.
EDIT:
String s1=new String("Rohit");
String s2="Rohit";
String s3=new String("Rohit").intern();
System.out.println(" "+(s2==s3)+" "+(s1==s2)+" "+(s1==s3)+" "+(s2==s3));
results as :true false false true
This makes me to think that without using intern() with new, there is no entry in pool for this object