Home > Java > javaTutorial > body text

Difference between valueOf and toString,(String) in Java

零下一度
Release: 2017-05-25 14:53:57
Original
1926 people have browsed it

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 provide a summary of commonly used conversion methods. Commonly used methods include Object.toString(), (String) the object to be converted, String.valueOf(Object), etc. These methods are analyzed one by one below.


Method 1: Use the Object.toString() method please see below Example:
Object object = getObject();
System.out.println(object.toString());

In this usage method, because there is a public method .toString() in the java.lang.Object class, this method can be called on any java object in the strict sense. But be careful when using it, you must ensure that the object is not a null value, otherwise a NullPointerException will be thrown. When using this method, usually the derived class will override the toString() method in Object.

Method 2: Use the type conversion (String) object method. This is a standard type conversion. , convert the object into a String type value. When using this method, it should be noted that the type must be convertible to 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. In addition, special care is required because it is defined as Object The syntax check will not report an error when an object of type is converted to String, which may lead to potential errors. Be extra careful at this time. For example:
Object obj = new Integer(100);
String strVal = (String) obj;
There will be an error at runtime because the Integer type is forced to be converted to the String type and cannot be passed. However,
Integer obj = new Integer(100);
String strVal = (String)obj;
If this is the format code, A syntax error will be reported.

In addition, because the null value can be cast to any java class type, (String)null is also legal.

Method 3: Using String.valueOf(Object) The basis of String.valueOf(Object) is Object.toString(). But it is different from Object#toString(). As mentioned in the previous analysis of method 1, when using the latter, it must be ensured that it is not null. But when using the third method, you don't have to worry about whether the object is a null value. In order to facilitate the explanation of the problem, let's analyze the relevant source code. The source code of String.valueOf(Object) in Jdk is as follows:
public static String valueOf(Object obj) {
return (obj == null) ? " null" : obj.toString(); }
From the above source code, we can clearly see the reason why there is no need to worry about null values. However, this also gives us hidden dangers. We should note that when object is null, the value of String.valueOf(object) is the string "null", not null! ! ! Remember to pay attention during use. Just imagine what problems may occur if we use statements like if(String.valueOf(object)==null){System.out.println("The value passed in is null!");}. Think again, when outputting to the console, what is the visual difference in the execution results of the following statements:

System.out.println(String.valueOf(null ));//It is the string "null"

System.out.println(null);//It is the empty value null
The output we see will be exactly the same thing: null, but do they mean the same thing?

Related recommendations】

1. Detailed explanation of valueOf method examples in java

2. valueOf, parseInt, toString in Java The difference between the three

3. In-depth understanding of the valueOf function and toString method

4. Introduction to the object conversion function toString() and valueOf()

5. Use toString() method to return the time as a string

The above is the detailed content of Difference between valueOf and toString,(String) in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!