Forums Register Login

String Intern

+Pie Number of slices to send: Send
What is the difference between the following two statements:

String str1 = new String("Sen");

String str2 = new String("Kumar").intern();
+Pie Number of slices to send: Send
Welcome to JavaRanch

I presume you are asking this as a question, rather than a recommendation, because you don't usually use the copy constructor for String.

To the API! If you need more explanation please ask again.

If there already is a String containing "Sen" the first statement will create a second String containing "Sen".

If there already is a String containing "Kumar" the second statement will create a new String, then replace it by the pre-existing String "Kumar."
***************************************************************************
Actually there already is a String "Kumar". You have written it in the code.
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 842 times.
Similar Threads
Significant different between == and equals()?
Why would this print true?
How many Objects ?
question regarding String.intern()
Quick == question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:45:02.