Scenario One: bad string stitching habits
The cause is this: a Daniel writes a lecture on the problem of Java string optimization, and he mentions: do not use the Strobj+othervalue method to convert Othervalue to string form, because the underlying operation will make you jump. So what is the underlying essence? What he meant was this:
For example: String s = "I have";
int total = 12;
Dog dog = new Dog (); //Suppose the dog class overrides the ToString method
String msg = s + total+dog;
At run time, the assignment statement for MSG executes as: msg = new StringBuilder (). Append (s). Append (total). Append (Dog.tostring ()). ToString ();
We found that the runtime created an anonymous StringBuilder object to stitch the string, store it into a buffer, and finally call the ToString method to return the result after stitching. Obviously, StringBuilder is used here, and consumes memory resources, it is not advisable. I find a lot of people like to write code like "+num", and so do I. So you need to be aware of it later, especially in a performance-sensitive environment. You should instead use String.valueof (value), or the ToString method of the wrapper type, such as double.tostring (value).
If you need to make frequent modifications to the string, use StringBuilder, or StringBuffer (thread-safe), to avoid a large number of intermediate string garbage fragments.
Scenario Two: Why one is true and one is False
A netizen mentioned the question:
String str = "ABC";
String str1 = "AB" + "C";
String str2 = "AB";
String STR3 = str2 + "C"; //Try running this code, you will find the first one is true the second is false, why?
here is my explanation:
This is due to the Javac compilation optimization. sTR1 is a concatenation of 2 string constants, constants must not change the amount, then in the compilation phase, Javac has the guts to simplify it: STR1 is optimized to: str1= "abc", and because STR is also "ABC", So str and STR1 share the memory according to "ABC", so str and str1 all point to "ABC". because = = is a shallow comparison, the memory address of the string is compared, so they are equal.
STR3 's stitching contains a variable str2, and the variable is dynamically determined at runtime, so Javac is not afraid to optimize it when compiling. that is, STR3 will new the string object "ABC" in the heap at run time, obviously its address is not the same as the address of the previous "ABC", so it is false.
I decompile the above program after exporting the jar, validating my conjecture, the result
To further substantiate my conjecture, I changed the code above to read:
String str = "abc"final String str2 = "AB"= str2 + "C"; = = STR3); Print out True
Then the question comes, not that STR2 is a variable. How to print this time is true too? Note that I added the modifier final to str2, declare str2 bit constant, then the same reason, Javac can also ensure that str2 will not change, so STR3 optimization to: str3= "abc".
To decompile a class file:
Note: do not use = = to compare strings, because strings are reference types, = = Compare memory addresses instead of string contents. Please use Equals () or equalsignorecase ().
Only string constants are shared memory, and string variables, or strings that are dynamically generated at run time, are not.
About Java string Compilation optimization issues