How to realize the Deep copy of string by java
This article mainly introduces java how to achieve in-depth copy of the string, the article is very detailed, has a certain reference value, interested friends must read it!
How to achieve a deep copy of a string?
Because strings are immutable, you can directly use the "=" operator to copy one string to another without affecting each other.
Public class JavaStringCopy {public static void main (String args []) {String str = "Silent King II"; String strCopy = str; str = "Silent King III"; System.out.println (strCopy);}}
The output is as follows:
Silent King two
This example is almost no different from the previous example that proved that the string is immutable, right? This is really because the string is immutable, and if it is a mutable object, the depth copy should be noted, and it is best to use the new keyword to return the new object.
Public Book getBook () {Book clone = new Book (); clone.setPrice (this.book.getPrice ()); clone.setName (this.book.getName ()); return clone;} these are all the contents of the article "how to make a deep copy of a string in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!