Several forms of object to string
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. Each of these methods is analyzed below.
Method 1: Use the Object.ToString () method
Take a look at the following example:
Object object = GetObject ();
System.out.println (Object.ToString ());
In this method of use, because the public method is already in the Java.lang.Object class. ToString (), this method can be called for any Java object that is strictly meaningful. Note, however, that object must be guaranteed to be not a null value, otherwise the NullPointerException exception will be thrown. With this approach, the derived class typically overrides the ToString () method in object.
Method 2: Use the type conversion (String) object method
This is the standard type conversion, which converts an object to a value of type string. When using this method, it is important to note that the type must be able to be turned into a string type. Therefore, it is best to use instanceof as a type check to determine whether it can be converted. Otherwise it is easy to throw calsscastexception exceptions. In addition, it is particularly necessary to be especially careful that the syntax check does not cause an error when the object that is defined as type objects is turned into a string, which can lead to potential errors. Be extra careful at this point. Such as:
Object obj = new Integer (100);
String strval = (string) obj;
Errors will occur at run time because the integer type is cast to a string type and cannot be passed. But
Integer obj = new integer (100);
String strval = (string) obj;
In the case of a format code, a syntax error will be reported.
In addition, because null values can be cast to any Java class type, (String) NULL is also legal.
Method 3: Use String.valueof (Object)
String.valueof (Object) is based on object.tostring (). But it's different from object.tostring (). As mentioned in the previous analysis of Method 1, it is necessary to ensure that the first type is not NULL. However, with the third approach, you will not have to worry about whether object is a null value. For the sake of explaining the problem, let's analyze the relevant source code. JDK string.valueof (Object) source code is as follows:
/**
* Returns The string representation of the Object argument.
*
* @param obj an Object.
* @return If the argument is null and 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 very clear to see the null value without worrying reasons. However, this also gives us a hidden danger. We should note that when object is null, the value of String.valueof (object) is the string "null" instead of null! in the process of using it. Imagine if we were to use
if (string.valueof (object) ==null)
{
SYSTEM.OUT.PRINTLN ("Incoming value is null!");
}
Such a statement will likely be problematic. Think again, when outputting to the console, visually the following statements differ in the result of the execution:
SYSTEM.OUT.PRINTLN (string.valueof (null));
SYSTEM.OUT.PRINTLN (NULL);
The output we see will be identical: null, but do they mean the same thing?
determine if a string is emptyS is a string that determines how empty it is: if(Null==s||"". Equals (s)){......}
Note: Here's null==s and "". Equals (s) do not write S==null and s.equals (s), because "" This value is already determined, predictable, and S is unknown, so when used carelessly s.equals ("") There will be nullpoint exceptions. Although not here, because there is an if (null==s), but the habit is not related to the use there. The Equals method is not necessarily the same, including many other treatments, and if the problem is handled with a certain value, it will be much less problematic than the indeterminate processing.
object to string in Java