Java string class

Source: Internet
Author: User

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!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.