Although String implements CharSequence, Java does not allow this. What is the reason for this design decision?
Although String implements CharSequence, Java does not allow this. What is the reason for
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The decision to disallow that was made because it’s not type-safe:
And now I’ve tried to put a
MyEvilCharSequenceinto aStringmap. Big problem, sinceMyEvilCharSequenceis most definitely not aString.However, if you say:
Then that works, because the compiler will prevent you from adding non-
nullitems to the map. This line will produce a compile-time error:See here for more details on generic wildcards.