What is the difference between string.valueof (obj), (String) obj and obj.tostring () in Java

Source: Internet
Author: User

Method 1: Use the Object.ToString () method

In this method of use, because the public method is already in the Java.lang.Object class. ToString (), this method can be called for any Java object that is strictly meaningful. note, however, that object must be guaranteed to be not a null value, otherwise the NullPointerException exception will be thrown. with this approach, the derived class typically overrides the ToString () method in object.

Method 2: take the type conversion (String) of the object method, which is the standard type conversion and converts the object to a String-type value. When using this method, it is important to note that the type must be able to be turned into a string type. Therefore, it is best to use instanceof as a type check to determine whether it can be converted. Otherwise it is easy to throw calsscastexception exceptions. In addition, it is particularly necessary to be especially careful that the syntax check does not cause an error when the object that is defined as type objects is turned into a string, which can lead to potential errors. be extra careful at this point. Such as:
Object obj = new Integer (100);
String strval = (string) obj;
Errors 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, because null values can be cast to any Java class type, (String) NULL is also legal.

Method 3: using String.valueof (object) string.valueof (object) is based on object.tostring (). But it's different from object#tostring (). In the previous analysis of Method 1, it was mentioned that the latter should be guaranteed not to be null. However, with the third approach, you will not have to worry about whether object is a null value. For the sake of explaining 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 code can be very clear to see the null value without worrying reasons. However, this also gives us a hidden danger. We should note that when object is null, the value of String.valueof (object) is the string "null", not null!!! remember to keep in mind when using the process. Imagine if we were to use if (string.valueof (object) ==null) {System.out.println ("the value passed in is null! ”);} Such a statement will likely be problematic. Think again, when outputting to the console, visually the following statements differ in the result of the execution:

SYSTEM.OUT.PRINTLN (string.valueof (null));//is the string "null"

SYSTEM.OUT.PRINTLN (null);//null value NULL
The output we see will be identical: null, but do they mean the same thing?

What is the difference between string.valueof (obj), (String) obj and obj.tostring () in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.