Why String class is declared as final in Java?
I'll answer
Earn 20 gold coins for an accepted answer.20
Earn 20 gold coins for an accepted answer.
40more
40more
Works at Oracle, Lives in Redwood City, CA
String is a very core class in Java, many things rely on it working a certain way, for example being immutable. Making the class final prevents subclasses that could break these assumptions. Note that, even now, if you use reflection, you can break Strings (change their value or hashcode).
2023-05-10 14:10:00
评论(499)
Helpful(122)
Helpful
Helpful(2)

Zoe Young
QuesHub.com delivers expert answers and knowledge to you.
String is a very core class in Java, many things rely on it working a certain way, for example being immutable. Making the class final prevents subclasses that could break these assumptions. Note that, even now, if you use reflection, you can break Strings (change their value or hashcode).