1. = and equal () of string ()
When the string is equal, = determines whether the address is the same, equal () determines whether the character value is the same. Most of the time = is the same as equal. This is because the string object is in the constant mode. If you do not explicitly Create a New String object, the New String object is stored in a buffer by default in Java, then, each time you determine whether the buffer zone already has this object, if so, then the string object created with the same character value will also point to the address of the originally created object with this character value. That is to say, when the character values are the same, the geological conditions are the same in most cases. = The same effect as equal. However, when the object is generated by STR = new string ("ABC") instead of directly assigning values like STR = "ABC", or after some string connection processing, or, if it is generated by stringbuffer and other objects, a new address is opened in the memory. At this time, the = and equal () results are different.
Is it a little complicated? Here we need some understanding about memory, stack, and Object Storage. I don't want to be entangled in the discussion of this issue. If you cannot understand it, remember to use equal () instead of =, as for when to use =, I think you will naturally understand when you need it. In fact, we seldom need to use = for string judgment.
2. About Str. Equal ("ABC") and "ABC". Equal (STR)
There seems to be a lot of arguments. The first constant may be followed by most people's habits and our logic thinking. However, you need to determine whether another STR is null. Otherwise, exceptions may occur. The other method does not need to judge whether the statement is null. In my personal preferences, I prefer the latter method.
3. null of string
/**
* test if Java's string is null
* Create Date: 2009-6-3
* Author: administrator
*/
Public static void testnull () {
string a = NULL, B = NULL, c = "Haha ";
system. out. println (A = NULL);
system. out. println (A + B + C);
}< br> method running result:
true
nullnull
so when merging strings, do not forget to judge null. Otherwise, the result will not be refreshing!