is an empty string considered as a new String object ? e.g. tell how many new string objects are created ? String s="abc"; String b="kau"; System.out.println(s+" "+b); so, the ans should be 2 or 3 ?
Just a note on terminology: an "empty String" contains no characters. You can make one like this: s = "", or like this: s = new String(). The one used above is not empty.
Finally what is the answer??? Is it 3 or 4 or 5. I think it is 5...because in string conctatenations like s+" "+b we also have the string object "abc " formed. So I think there are 5 "abc" "kau" " " "abc " "abc kau" Please let me know if I am correct. Thanks Lalitha
Only 4 are created. A StringBuffer is created, the three strings are appended to it. Then StringBuffer.toString is invoked returning the resulting string. Because this process might depend on the JVM, these kind of questions are not likely to appear in the exam.