public class TestDemo{
public void main(String[] args){
//对象保存在堆中
Integer i =new Integer(5);
Integer j = new Integer(5);
System.out.println(i==j);//false 内存地址
System.out.println(i.equals(j));//true 内存里保存的数据
//对象保存在常量池中
Integer i =5; //超过127以后(128)源码中看到缓存了-128 ——127的数据
Integer j =5
System.out.println(i==j);//true //false
System.out.println(i.equals(j));//true //true
}
}
启动时已经缓存好了
Boolean Byte 全部缓存
Character <=127 缓存
Short Integer Long 都-128 ——127缓存
Float Double 无缓存 (不精确)
String s1="hello";
String s2="hello;
System.out.println(s1==s2);//true
String s3=new String("hello");//实际在内存存了6个还有/0.字符串的结束标志
String s4="hello";
System.out.println(s3==s4);//false
在堆中heap中有String pool(缓存池)第一句代码执行时,先去缓存池找有没有hello,没有再创建。
当第一次遇到时会缓存
String pool