Original address: Http://blog.laofu.online/2017/08/18/String-In-dotnet-Java/Java Strings encountered problems
In writing the Java program encountered a problem, and it is this problem caused me to think about the string, the Java sample code is as follows:
Public void Teststr (String success) { if(success== "Y") { System.out.println ("Equal"); } Else { System.out.println ("not Equal"); } }
The above function is simple, but will show different results as the invocation is different:
Public void Callmethod () {teststr ("Y"); // Equal Teststr ("YY". Substring (0,1)); // Not Equal}
For such a result, we can first think of a question: "= =" The role of this operator?
-
Whether the comparison value is the same for the underlying data type (action is the same as equal)
-
For reference types, the same address is compared
But if we understand the code above, we also have to understand the mechanics of strings in Java. Since strings are commonly used types, in order to ensure performance, there is a "pool" concept when designing strings.
-
Once a character has been created, it is no longer changed, and the operation of the character is created as a new string object
-
Before the character is created, find out if the same string already exists in memory, and if there is a direct address to the current object, no new object is created directly
So for the above code, because at the beginning of the "Y" string that has been created, all "Y" after birth is referring to our current "Y", so we can understand why the first one is the print Equal and the other is the print not Equal.
. NET, how to handle
And for. NET, the principle of the string is roughly the same, if the same code, but the result of the operation is not the same as Java:
We know that the. Net string is also a reference type, but when "= =" acts on two reference types, the comparison is the address, but the. NET, the comparison is the value. Thanks to this. NET to the "= =" overload, string source code. If you want to compare addresses, use object. The ReferenceEquals () function.
Public Static BOOL operator = = (string A, string b) {return string.equals (A, b);}
For. NET operator overload, the individual feels more close to everyday usage, because in the process of encoding, most of the usage scenarios in a string are values, not references. And for Java, to ensure that the operation of the original, less human packaging interference, use is to pay attention to distinguish, accustomed to feel more reasonable.
Several question strings are reference types, so why not use new to create objects?
A string is a special reference object that is declared to create an object, and if new is used, the object is created repeatedly (Java can be created with new. NET is not allowed to do so), wasting memory, as follows:
String str=New string ("1234"); String str1= "1234";
Both are defined in the same way, but when new is used, additional memory space is allocated.
A string is a reference type, but cannot modify its value when it is passed? Are there any other reference types that are different?
Public void Callmethod () { String str= "abc"; Addsuffix (str); System.out.println (str); // Print out ABC } publicvoid Addsuffix (String x) { x=x+ "123"; }
When we go to call this function, we find that the value of STR has not changed. Because when the Addsuffix function is called, str copies itself as a copy to the parameter x, when assigning a value to X, the system re-creates a character object, gives the referenced address to X, where the object is recreated instead of modifying the original string object (the string cannot be changed). Two ways are as follows:
The 48th article says. NET vs. Java strings