In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string. This article will make a summary of the common conversion methods. Common methods are object#tostring (), (String) objects to be converted, string.valueof (object), and so on. The following is an analysis of these methods.
Method 1: Using the Object#tostring () method
Take a look at the following example:
Objectobject=getobject ();
System.out.println (Object.ToString ()); Note 1
In this use method, because there is already a public method in the Java.lang.Object class. ToString (), you can call this method for any Java object in a strict sense. Note, however, that you must ensure that object is not a null value, or you will throw a NullPointerException exception when used. When this method is used, the derived class usually overwrites the ToString () method in object.
Method 2: Use the type conversion (String) object method
This is the standard type conversion, which converts object to a string type value. When using this method, it is important to note that the type must be able to be converted to a string type. Therefore, it is best to use instanceof to do a type check to determine whether it can be converted. Otherwise, it is easy to throw calsscastexception exceptions. In addition, it is necessary to be particularly careful that a grammar check does not cause an error when the object is defined as objects of type object when it is turned into a string, which can lead to potential errors. Be extra careful at this time. Such as:
Objectobj=newinteger (100);
String strval= (string) obj;
An error will occur at run time because the integer type is cast to a string type and cannot be passed. But
Integerobj=newinteger (100);
String strval= (string) obj;
In the case of a format code, a syntax error will be reported.
In addition, a null value can be cast to any Java class type, and (String) NULL is also legal.
Method 3: Using String.valueof (Object)
String.valueof (Object) is based on object#tostring (). But it differs from object#tostring (). In the analysis of previous Method 1, it was mentioned that the latter should be used with a guarantee of not being null. However, when using the third method, you will not have to worry about whether the object is a null value. To illustrate the problem, let's analyze the relevant source code. JDK string#valueof (Object) source code is as follows:
/**
*ReturnsthestringrepresentationoftheObjectargument.
*
*@param obj anObject.
*@return iftheargumentisnull,thenastringequalto
*"null";otherwise,thevalueof
*obj.toString()isreturned.
*@see java.lang.Object#toString()
*/
publicstaticStringvalueOf(Objectobj){
return(obj==null)?"null":obj.toString();
}
From the above source can be very clear to see null values do not worry about the reasons. However, this also gives us the hidden danger exactly. We should note that when object is null, the value of String.valueof (object) is the string "null" instead of the null!!! Be mindful of the use of the process. Imagine if we use the IF (String.valueof (object) ==null) {System.out.println ("Incoming value is null! ”);} What might be the problem with such a statement. Again, when you output to the console, visually the following statements differ in the results of execution:
System.out.println ((Object) null) (string.valueof); NOTE 2
System.out.println ((Object) null); NOTE 2
The output we see will be exactly the same: null, but do they mean the same thing?
Note 1: Light on the use of SYSTEM.OUT.PRINTLN, the statement System.out.println (Object.ToString ()), the Change to System.out.println (object), better. The use here is primarily to illustrate the use of object#tostring ().
Note 2: It is recommended that you use SYSTEM.OUT.PRINTLN (string.valueof (Object) null), SYSTEM.OUT.PRINTLN (string.valueof (NULL)) is not recommended, and when overloaded methods are used , it is a good habit to do so.
These are some of the summaries of converting object objects to strings.