Differences in Java Tostring(), (string), valueof() method

In the actual development and application of Java projects, the basic function of converting objects into strings is often needed. This paper will summarize the common conversion methods. Common methods are object Tostring(), (string) the object to be converted, string. Valueof (object), etc. These methods are analyzed one by one below.

Method 1: use object For tostring() method, see the following example:

In this use method, because Java Public method already exists in lang.Object class Tostring(), so you can call this method on any strictly Java object. However, when using, you must ensure that the object is not null, otherwise a NullPointerException exception will be thrown. When using this method, the derived class usually overrides the toString () method in object.

Method 2: use type conversion (string) object method this is a standard type conversion, which converts an object to a value of string type. When using this method, it should be noted that the type must be able to be converted to string type. Therefore, it is best to use instanceof to make a type check to determine whether it can be converted. Otherwise, it is easy to throw a calsscastexception exception. In addition, you should be careful because it is defined as an object type When the object is converted to string, the syntax check will not report an error, which may lead to potential errors. Be extra careful at this time. For example:

An error will occur at run time because the integer type cannot be cast to a string type. But,

If it is a format code, a syntax error will be reported.

In addition, since 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 is different from object #tostring(). As mentioned in the previous analysis of method 1, when using the latter, it is necessary to ensure that it is not null. However, when using the third method, you will not have to worry about whether the object is a null value. In order to illustrate the problem, let's analyze the relevant source code. String in JDK The source code of valueof (object) is as follows:

From the above source code, you can clearly see the reason why you don't have to worry about null value. However, this also gives us hidden dangers. We should note that when the object is null, string The value of valueof (object) is the string "null", not null!!! Remember to pay attention during use. Imagine if we use

If (string. Valueof (object) = = null) {system. Out. Println ("the value passed in is null!");} What problems may occur with such statements. Think again about how the following statements visually differ in execution results when outputting to the console:

The output we see as like as two peas: null, but do they mean the same thing?

The above is the difference in Java introduced by Xiaobian Tostring(), (string) and valueof() methods. I hope they can help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>