In the actual development and application of Java projects, it is often necessary to use objects to convert objects to String. This article will summarize the commonly used conversion methods. Common methods have object # toString (), (String) to convert objects, String.Valueof (Object), etc. These methods are analyzed by one by one below.
Method 1: Please see the following example using Object # toString ().
Object object = getObject ();
System.out.println (Object.toString ());
In this method of use, because the public method has been in the java.lang.Object class. TOSTRING (), so this method can be called for any Java object in any strict sense. But when you use, you must ensure that Object is not a null value, otherwise the NullPointerexception will throw an exception. When using this method, it is usually derived to override the toString () method in the object of Object.
Method 2: Use type conversion (String) Object method This is the standard type conversion, convert Object to the value of String type. When using this method, it is necessary to note that the type must be able to convert a string type. Therefore, it is best to use instanceof to make a type check to determine if it can be converted. Otherwise it is easy to throw the CalssCastException exception. In addition, it is necessary to be particularly careful because objects defined as an Object type are syntax checking and not reporting an error, which may cause potential errors. Be careful at this time. Such as:
Object obj = new integer (100);
String str remote = (string) OBJ;
It will be wrong at runtime because the Integer type is forced to convert to the String type, which cannot be passed. but,
Integer Obj = new integer (100);
String str remote = (string) OBJ;
If the format code will report the syntax error.
In addition, because NULL values can be forced to convert to any Java class type, (String) NULL is also legal.
Method 3: The basis of using String.Valueof (Object) string.valueof (Object) is Object # toString (). But it is different from Object # toString (). In the analysis of the front method 1, it is not necessary to ensure that the latter is not NULL. However, when using the third approach, you will not have to worry about whether Object is a null value. To facilitate the problem, let's analyze the relevant source code. JDK String # valueof (Object) The source code is as follows:
/ **
* Returns the string representation of the object argument.
*
* @Param Obj an Object.
* @Return if the argument is null, THEN A STRING Equal To
* "Null"; OtherWise, The Value of
* Obj.toString () is returned.
* @see java.lang.Object # toString ()
* /
Public static string valueof (Object obj) {
Return (obj == null)? "null": obj.tostring ();}
From the above source code can be clearly seen that the NULL value does not have to worry. However, this is also just gave us hidden dangers. We should notice that when Object is null, the value of String.Valueof (Object) is a string "null" instead of null! ! ! Remember to pay attention during use. Imagine if we use IF (String.Valueof (Object) == null) {system.out.println ("Incoming Value is NULL!");} What questions may occur like this. Think about it, when you output it to the console, what is the difference in the execution of the following statement:
System.out.println (String.Valueof (null);
System.out.println (NULL);
The output we have seen will be the same thing: null, but do their meaning?
The above is some summary of converting the Object object to String.