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: Using Object # toString () method
Please see the example below: Object object = getObject (); system.out.println (Object.toString ()); in this method of use, because there is already a public method in 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: Type conversion (String) Object method
This is the standard type conversion, converts Object to the value of the 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 strign = (string) OBJ; it will be wrong at runtime because the Integer type is forced to convert to a String type, and cannot be passed. However, Integer Obj = new integer (100); string strital = (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: Using String.Valueof (Object)
String.Valueof (Object) The foundation 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 in String # valueOf (Object) source code 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". @ 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 see the reasons why null values don't 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 again, when you output it to the console, it is visually different in the results of the following: system.out.println (String.Valueof (null); system.out.println (null); we see The output will be exactly the same: NULL, but is their meaning? The above is some summary of converting the Object object to String. Comments on this article
9CBS netizen (2004-08-09) to @ 2004.8.9 11:50 61.171.232. * Post a review of users' reply: Thank you for your participation. System.Out.println (Object.toString ()); a sentence is hereby here to explain the usage of Object # toString (). About this point, the rest I agree with you. Another, system.out.println (String.Valueof (null); system.out.println (null); you said it is "actually exactly the same", personal holds different views. System.out.println (String.Valueof (null)); (this means of system.out.println (String.Valueof ((Object) null);) execution effect is equivalent to system.out.println ("null" ), The null ---> "null" is implemented through the string.valueof () method. System.out.Println (NULL) The implementation process is as follows: First call: PrintStream # Println (String) method, then enter the PrintStream # Print (String) method, the source code of this method is: public void print (String s) { IF (s == null) {s = "null";} Write (s);}. It can be seen that NULL ---> "null" is completed in the print () method, so both are different.
9CBS netizen (2004-08-09) to @ 2004.8.8 11: 3 61.235.89. * Netizen a reply :: Thank you very much. For the examples of this article, in order to output normally in all cases, system.Valueof (null); statement is replaced with system.out.println (String.Valueof ((Object) null) More secure. I am in the same name in 9cbs blog.
9CBS netizen (2004-08-09) Object object (); system.out.println (Object.tostring ()); paint sauce is not necessarily good should be system.out.println (Object); call String.ValueOfsystem. Out.Println (String.Valueof (null); system.out.println (null); actually exactly, see String.Valueof (null) in 1.4 is completed, because 1.4 has string.valueof (Object) and String.Valueof (char []). 1.3 can be 9CBS users (2004-08-08) System.out.Println (String.Valueof (null); will throw an exception, I use JDK1.4.1.