Several common methods of java objects to Java String

zhaozj2021-02-16  152

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 ()); Note 1

In this method of use, because the public method has been in the 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 str remote = (string) OBJ;

It will be wrong at runtime because the Integer type is forced to convert to the String type, which cannot be passed. but,

Integer Obj = new integer (100);

String str remote = (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 String # valueof (Object) The source code is 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"; 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 clearly seen that the NULL value does not 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 about it, when you output it to the console, what is the difference in the execution of the following statement:

System.out.println (String.Valueof ((Object) null); Note 2

System.out.println (NULL);

The output we have seen will be the same thing: null, but do their meaning?

Note 1: Light uses the use of System.out.Println, statement system.out.println (Object.toString ()); change to System.out.Println (Object); better. As used herein, it is mainly to illustrate the usage of Object # toString ().

Note 2: Recommended using system.out.println (String.Valueof ((Object) null); not recommended using system.Valueof (null); this is a one when using the overload method Good habit.

The above is some summary of converting the Object object to String.

转载请注明原文地址:https://www.9cbs.com/read-9157.html

New Post(0)