Java.tostring (), (String), string.valueof () difference __java

Source: Internet
Author: User

First of all, what is the role of their three: no special effect, that is, let us get the object or parameter type, as required to the form of a string.
String: There is no doubt that this is a strong transfer form, simple and convenient, high efficiency. Java programmers may see high efficiency and perhaps some excitement, but it has his bad, that is limitations. There is nothing in the Java world without faults, there are always some good things and some bad. The bad thing is, he can only be a strong turn is a string of things, if the original is not a string, then it will be an error. Like what:

1 2 3 4 5 Boolean Boolean1 = true; String str3 = (string) boolean1; This line of code will error, compiled but String STR4 = boolean1.tostring (); Normal String STR5 = string.valueof (boolean1); Normal

It is clear from the example above that the string's strong turn is easy, so it has a lot of limitations. Be careful when you use it. ToString (): In the API documentation, it says, returns the object itself (it's already a string ...). )。 According to its meaning, the general object or parameter is a method of ToString (), only to note that in a parameter defined as type int is not this method. And when the argument is empty. The ToString () method reports a null pointer exception, which is a bad place to use when you need to think about it. Like what:

1 2 Object obj = GetObject (); Syystem.out.println (Obj.tostring ());

As the above code indicates, if obj is not empty, then the normal compile, then if obj took out the empty words, then will report out null pointer exception. The most important part of the play string.valueof (): This method is static, directly through the string call, can be said to be perfect, but usually not used to write it, such a realization to avoid the previous two deficiencies and shortcomings. First, let's look at his internal implementation mechanism:

1 public static String valueof (Object obj) {return (obj== null)? "Null": Obj.tostring ()};

In the interior is done for the null judgment, so will not report a null pointer exception.

Summarize:
The use of these three, individual feel should use string.valueof () way. Such use is safe and reliable, and does not lead to anomalies.

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.