一.不可变类和不可变对象 Normally,you create an object and allow its contents to be changed later.However ,occasionally it is desirable to create an object whose contents cannot be changed once the object has been created.We call such an object as immutable obje
1 将方法或类声明为final主要目的是:确保它们不会在子类中改变语义.String类是final类,这意味着不允许任何人定义String的子类. String基本约定中最重要的一条是immutable. 但是假如String没有声明为final, 那么你的StringChilld(声明为final就不可被继承)就有可能是被复写为mutable的,这样就打破了成为共识的基本约定. 2 String源码前几行 public final class String implements java.
部分内容转自知乎:https://www.zhihu.com/question/31345592 从自己的理解进行加工,压缩. String本质上是一个final类 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value