iteretor与Collection的关系
public interface Collection extends Iterable
public interface Iterable {
Iterator iterator();
}
String为什么是不可变字符串?
它是final修饰的
JDK1.8中
public final class String
implements java.io.Serializable, Comparable, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
好处是什么?
1.不需要额外的对象空间,相同字符串指向常量池中同一个变量
2.hashmap的key使用string类型保证了hashcode值的不变
3.不可变所以线程安全,作为各类参数传递