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