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: using the Object.ToString () method, see 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: take the type conversion (String) of the object method, which is the standard type conversion and converts the object to a String-type value. 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: using String.valueof (object) string.valueof (object) is based on object.tostring (). But it's different from object#tostring (). In the previous analysis of Method 1, it was mentioned that the latter should be guaranteed not to be 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:
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", not null!!! remember to keep in mind when using the process. Imagine if we were to use if (string.valueof (object) ==null) {System.out.println ("the value passed in 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));//is the string "null"
SYSTEM.OUT.PRINTLN (null);//null value NULL
The output we see will be the exact same thing: null, but the meaning is not the same.
Differences in Java. toString (), (String), ValueOf () method