I know this may not make any sense, but this is just my experiment. There are 3 types (as I know) that support the subString () method. I will not do the general method as follows:
public static <T extends String & StringBuilder & StringBuffer> T substr3(T str) { if (str.length() > 3) { return (T) str.substring(0, 3); } return str; }
It will not compile this way because you can use as many interfaces as you need, but only one class as a limited type. This method should work fine for these three types: String, StringBuilder, StringBuffer, but there are questions: how to set these 3 types as limited types?
source share