During development, we often encounter the problem of converting data from collection classes List and Map to String. If we do not handle it well here, we often encounter null pointer exception java.lang.NullPointerException. Here is a summary of commonly used conversions to String. method, and the problem of how to judge null after conversion.
Forcible type conversion, the object obj is null, and the result is also null, but obj must ensure that its essence is a value of String type, that is, a converted value.
For example, you cannot force conversion (String) 123
When calling the toString method of an object, you must ensure that this class or the parent class has overridden the toString method of the Object class. If the toString method has not been overridden,
The toString method of the Object class will be called by default and return getClass().getName() + '@' + Integer.toHexString(hashCode()),
It is not the actual string representation of obj. At the same time, it must also ensure that the object obj cannot be null, otherwise call toString The method will report a null pointer exception java.lang.NullPointerException.
The object obj is null, and the conversion result is the string "null", otherwise, return ## The value of #obj.toString().
# be used. ## StringUtils.isBank(CharSequence cs) instead, str.equals("null") should be used.
Already know that obj is of String type:
Use
Method 1to convert to String. After converting to String, the null condition is: if (objStr != null) Use method 2 with caution
The above is the detailed content of Common methods for converting objects to String in Java. For more information, please follow other related articles on the PHP Chinese website!