[GISER && JAVA] the basis of blind spots series immutable string String-

  Lift the strings, we should be very familiar with, a standard predefined classes provided as JAVA. In everyday use, flexible string String variability and rich API interface gives us a great convenience, but it is worth noting that, in fact, the String class is not in itself provide a modified method, also called the String class it is [ immutable string] .

  I am not feeling a little bit and not the same impression? Indeed, if we want to modify a string, there are a number of ways, such as:

String target = 'oldString'

target = target.replace("old", "new"); // "newString" target = "new"+target.substring(3); // "newString"

  These are not all of the target was revised yet? Yes, so get the result returned is indeed a string we need. This target can have an either string pointed to by a pointer to the string of target.

  When we get oldString generated, and can be seen as a continuous distribution in the region in memory address for storing the value of oldString and point newString value with a target variable, when we modify, equivalent to re-open a new memory address storage newString, then the target variable redirected newString, so from the point of view, it may indeed appear to replace the oldString characters, but in fact is paired to target a new string object. Why do you do that? General view, modify the value of an object, to be more simple than a new object more efficiently.

  In the "JAVA core technology Volume I" is explained this way: "JAVA designers think share of the high efficiency far better than the extract, concatenate strings brought low efficiency Personally, I was so understandable in the JVM. stack part of the model, allocates memory storage string object instances, the objects stored in the heap memory is shared by the threads, in comparison, copying, etc. string of higher frequency operation, share these strings may then modify the string with Competition to the efficient and more valuable.

  That problem again, this endless creation of a new String object, would not run the program will bring a lot of pressure? Do not forget, this is JAVA. In the garbage collection in the JVM, the object is not referenced will be a period of time for recycling. So you can not worry too much trouble to bring objects that are created.

Guess you like

Origin www.cnblogs.com/escage/p/12593976.html