How-Do/Can I set the value of a String object in Java (without creating a new String object)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are no “set” methods on
String. Strings are immutable in Java. To change the value of aStringvariable you need to assign a different string to the variable. You can’t change the existing string.Assigning doesn’t create a new object – it copies the reference. Note that even if you write something like this:
it won’t create a new string object each time it is run. The string object will come from the string pool.