Some understandings of the Java String class about = =, equals, Null_java
Source: Internet
Author: User
1, String of = = and Equal () In the equality of the string judgement, = = Judge whether the address is the same, equal () to determine whether the character value is the same. Most of the time the results are the same as the equal (). This is because the string object is invariant, and if you are not explicitly new to a string object, Java saves the string object by default by putting the newly generated string object in a buffer, and then each time the buffer has the object, if so, The string object that is then established with the same character value also points to the address that was originally established as the character value object. In other words, when the character values are the same, most of the time the geology is the same. = = The effect is the same as the equal (). However, when an object is generated by str = new String ("abc") rather than directly str = "ABC", or through some string concatenation, or through objects such as StringBuffer, the new address is opened in memory, this time = = and Equal () The results are different.
Is it a little bit complicated? Here you need some understanding of memory, stack, object preservation. I don't want to dwell on the discussion of this question. If you can't understand it, just remember that if you want to determine whether two strings are equal in character and have no other requirements, then use equal () instead of = = when you need to use = =, I think when you need, you will naturally understand. In fact, we rarely need to use = = for the judgment of strings.
2, about Str.equal ("abc") and "abc". equal (str)
This seems to be a lot of controversy, the first constant written in the back may be in line with the habits of most people, but also in line with our logical thinking. But it takes more than one STR to determine whether it's null or not. Otherwise, there may be anomalies. The latter type of writing does not require much of a judgment about whether or not to be null. In my personal preference, I prefer the latter one.
3. Null for string
/**
* Test Java string for null condition
* Create date:2009-6-3
* Author:administrator
*
*/
public static void Testnull () {
String a= null,b = null,c= "hehe";
System.out.println (A = = null);
System.out.println (A+B+C);
}
method to run the result:
True
Nullnull hehe
So we should pay attention to do string merging operations, do not forget to judge Null, otherwise the result will not let you cool ah!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service