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, see the following example:
Object object = GetObject ();
System.out.println (Object.ToString ());
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: using the type conversion (String) object method, which is a standard type conversion, converts object to a String of values. 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:
Object obj = new Integer (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
Integer obj = new integer (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: the basis for using String.valueof (object) string.valueof (object) is 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:
public static String valueof (Object obj) {
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" rather than 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 (string.valueof (null));/is the string "null" SYSTEM.OUT.PRINTLN (null);/is a null value NULL
The output we see will be exactly the same: null, but do they mean the same thing?