将字符串转换为字符序列

And*_*mov 2 java string casting charsequence

String显式转换的目的是什么CharSequenceString本身实现CharSequence接口。

CharSequenceSpring 4.x从 1.4 开始支持 Java 6+ 。

Spring框架的代码片段:

public static boolean hasText(String str) {
    // Why do we cast str to CharSequence? 
    return hasText((CharSequence) str);
}

public static boolean hasText(CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }

    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

use*_*421 5

这样就不会无限递归。该方法实际上可以被删除。它可能只是为了向后兼容。