Java – is there a way to pass a string to a mutable method?
•
Java
I know that string variables are immutable in Java by default But is there any way to set a string variable or make it variable in some special way (when passing the string variable)?
Or at least temporarily allow Stirng to change?
I realized that I can change the value of string by assigning the modified immutable variable to the original variable, such as show:
String A = "Test"; A.toUpperCase(); //will make A upper case,but not save the new upper case string to a variable Sring A = A.toUpperCase; //will give me TEST and save it to A,replacing A
Is there any way to tell Java "Hey, especially this string, treat it as mutable"?
Solution
No, strings are immutable
If necessary, you should create your own variable structure to wrap strings or char arrays, or if it suits your needs, you can use structures such as StringBuilder
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码