The difference between toString () and string.valueof () in Android development.

Source: Internet
Author: User

In fact, many times the effect is similar

However, sometimes using. toString () will error, and using string.valueof () will not error

That means there's a difference between the two, in the nuances.

What is the difference between. ToString () and string.valueof ()?

The . toString () description is in the documentation for the JDK:

string.valueof (Object o) If the argument is null and then a String equal to "null"; Otherwise, the value of obj.tostring () is returned.

So, in this particular case, there's going to be an anomaly.

And look at string.valueof ()

1 publicstaticString valueOf(Object obj) {
2      return(obj == null) ? "null": obj.toString(); 
3 }

As you can see, the string.valueof () is also called the ToString () method of the object. So if you are using the null parameter, calling the ToString method will report an exception! The valueof () method returns null if it is null, not the string after the ToString () is returned.

We recommend that you choose string.valueof ()

Transfer from http://www.ztyhome.com/android-tostring-string-valueof-diff/

Related Article

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.