Several simple methods for converting objects into strings

Source: Internet
Author: User

In actual project development and application, the basic function of converting objects into strings is often used. This article will summarize the commonly used conversion methods. Common methods include Object. toString (), (String) Object to be converted, and String. valueOf (Object. These methods are analyzed one by one.

Method 1: Use Object. toString ()
See the following example:
Object object = getObject ();
System. out. println (object. toString ());
In this method, because public method. toString () already exists in the java. lang. Object Class, this method can be called for any java Object in a strict sense. However, you must ensure that the object is not null. Otherwise, an NullPointerException is thrown. When this method is used, the derived class usually overwrites the toString () method in the Object.

Method 2: Use the type conversion (String) object Method
This is a standard type conversion that converts an object to a value of the String type. When using this method, you must note that the type must be converted to the String type. Therefore, it is best to use instanceof for a type check to determine whether conversion is possible. Otherwise, a CalssCastException is thrown. In addition, it is important to note that syntax check does not report errors when an Object defined as an Object is converted to a String, which may lead to potential errors. Be especially careful. For example:
Object obj = new Integer (100 );
String strVal = (String) obj;

An error occurs during running because the Integer type cannot be forcibly converted to the String type. However,
Integer obj = new Integer (100 );
String strVal = (String) obj;
Because a null value can only be assigned a value of the reference type and is not applicable to the value type, only (String) null is valid.

Method 3: Use String. valueOf (Object)
String. valueOf (Object) is based on Object. toString (). But it is different from Object. toString. As mentioned in the analysis in method 1 above, when using the first method, you must ensure that it is not null. However, when using the third method, you don't have to worry about whether the object is null. We should note that when the object is null, the value of String. valueOf (object) is a String "null", not null! Pay attention to it during use. Imagine if we use
If (String. valueOf (object) = null)
{
System. out. println ("the input value is null! ");
}
What may happen to such a statement. Think about the differences in the execution results of the following statements visually when outputting data to the console:
System. out. println (String. valueOf (null ));
System. out. println (null );
The output we see will be exactly the same: null, but do they have the same meaning?

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.