Java – instead of creating a new string object, print a string with edit characters using a char array
•
Java
The purpose of this code is to change the string "a happy" to "a hippy"
String originalStr = "A Happy"; char[] charOriStr = originalStr.tocharArray(); charOriStr[3] = 'i'; //simplified code,we are supposed to loop through the String
To print the modified string, I use:
System.out.println(charOriStr);
However, the recommended solutions are as follows:
String revised = new String(charOriStr); System.out.println(revised);
I'm a beginner and I'm not sure if I should follow the recommended solution as a best practice guide? Thank you for your guidance, thank you
Solution
It is obviously wrong to recommend such a person Since you already have a char array in your hand, you don't need to convert it to string again, just print it Because the println method you use will convert string to an internal char array again
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
二维码