ValueOf methods and strong turns in Java

Source: Internet
Author: User

Case1:object object to String

It should be emphasized that the string.valueof () method, when the argument is type is object, and the value is null when he handles the way

public static String valueOf (Object obj) {
return (obj = = null)? "Null": obj.tostring ();
}

So in the back of Chengdu will not error but logically may be wrong, the general habit of writing is Str!=null && str.length () >0

When it is obtained by str = string.valueof (obj), the above judgment condition is problematic.

Therefore, it is best to go to String str= (string) obj, when obj is null, STR is still null

Case2:object package type with basic type, (attention to Integer and long,double)

First, obj is assigned the value of the wrapper type that is the base type of the data to be known by instanceof.

So the direct use of strong turn can be, there is no need to use similar Long.parselong (string.valueof (obj)) Such methods, with this complex problem will have a problem is that cannot return value cannot contain null

If it is null, the exception is thrown directly because Null has been converted to a string in the String.valueof () method, and the Long.parselong () method will report an exception if it passes in a null string.

The Long.parselong (string.valueof (obj)) method is equivalent to (long) obj, which uses the basic type to strongly turn

A better approach would be to use (Long) obj to go strong, to judge and handle null cases in later programs.

CASE3: The wrapper type of the base type and the Vlaueof method of the string are not the same as the processing of the obj=null, the processing of the string.valueof (obj) returns the "null" string, and the wrapper type of the base type returns the null

It is emphasized that string is not a basic type, more than a basic wrapper type

Summary: 1 It is best to turn to a string when obj type is encountered

2 It is best to turn when you encounter the type of wrapper of the obj type to several times, and then when you need to determine its type first with instanceof

ValueOf methods and strong turns in Java

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.