Java Common string.valueof, toString, (String)

Source: Internet
Author: User


In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string.

Common methods are object#tostring (), (String) objects to be converted, string.valueof (object), etc.

The transformation of string types in Java is commonly used in methods that have three of the headings.


Brief introduction:

1, toString, you need to ensure that the method calls the class, method, variable is not NULL, otherwise it will report a null pointer.

2, string.valueof. This method is somewhat special when used. In general, if you are determining the type of null incoming, the string "null" is returned, and if NULL is passed directly, an error occurs.

3, (String) string type strong turn. It is necessary to ensure that the type can be turned into a string type.

Here valueof is highlighted.

String Testring=null;

Testring=string.valueof (testring);

System.out.println (testring);

In this way, because it is determined that the string type is passed in, no error occurs when valueof calls the corresponding constructor, and therefore, the string "null" is returned.

String.valueof (NULL);

However, if you change to the above notation, you are prompted with a null pointer.

Each of these methods is analyzed below. Method 1: Use the Object#tostring () method

Take a look at the following example:

Object object = GetObject (); System.out.println (Object.ToString ());

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: Use the type conversion (String) object method

This is the standard type conversion, which converts an object to a value of type string. 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.

However, the 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: Use 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)); SYSTEM.OUT.PRINTLN (NULL);

The output we see will be identical: null, but do they mean the same thing?

Java Common string.valueof, toString, (String)

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.