Integer i=new Integer(5);
Integer j=new Integer(5);
System.out.println(i==j);//fasle比较地址
System.out.println(i.equals(j));//true比较值
Integer i=5
Integer j=5
System.out.println(i==j);//true比较地址,IntegerCache会缓存-128至127,即5对应地址是Integer初始化就已经缓存好的
System.out.println(i.equals(j));//true比较值
Integer i=129
Integer j=129
System.out.println(i==j);//fasle比较地址,超出Integer缓存区间
System.out.println(i.equals(j));//true比较值
同时Integer有个使数字反转顺序的reverse方法,在小项目中后端可以用来加密金额之类的,在前端再次使用即可以反转回来
Integer j=new Integer(5);
System.out.println(i==j);//fasle比较地址
System.out.println(i.equals(j));//true比较值
Integer i=5
Integer j=5
System.out.println(i==j);//true比较地址,IntegerCache会缓存-128至127,即5对应地址是Integer初始化就已经缓存好的
System.out.println(i.equals(j));//true比较值
Integer i=129
Integer j=129
System.out.println(i==j);//fasle比较地址,超出Integer缓存区间
System.out.println(i.equals(j));//true比较值
同时Integer有个使数字反转顺序的reverse方法,在小项目中后端可以用来加密金额之类的,在前端再次使用即可以反转回来
String i="hello";
String j="hello";
System.out.println(i==j);//true比较地址,String在创建时会在String pool中去找是否有hello的字符串,如果有则从缓存中赋值给刚创建的对象,如果没有,则创建一个新的缓存,当第二次赋值相同的时候,即用的上一次创建的地址,String是边加载边缓存,跟Integer不一样
System.out.println(i.equals(j));//true比较值
String i=new String("hello");
String j="hello";
System.out.println(i==j);//fasle比较地址,new创建的是一个新的地址,跟缓存的是另一块地址
System.out.println(i.equals(j));//true比较值
String i=new String("hello");
String j=new String("hello");
System.out.println(i==j);//fasle比较地址
System.out.println(i.equals(j));//true比较值
String j="hello";
System.out.println(i==j);//true比较地址,String在创建时会在String pool中去找是否有hello的字符串,如果有则从缓存中赋值给刚创建的对象,如果没有,则创建一个新的缓存,当第二次赋值相同的时候,即用的上一次创建的地址,String是边加载边缓存,跟Integer不一样
System.out.println(i.equals(j));//true比较值
String i=new String("hello");
String j="hello";
System.out.println(i==j);//fasle比较地址,new创建的是一个新的地址,跟缓存的是另一块地址
System.out.println(i.equals(j));//true比较值
String i=new String("hello");
String j=new String("hello");
System.out.println(i==j);//fasle比较地址
System.out.println(i.equals(j));//true比较值
Boolean缓存全部,即true,false
Byte缓存全部,即-128至127
Characte缓存<=127
Short、Long缓存-128至127
Float、Dubbo没有缓存
Byte缓存全部,即-128至127
Characte缓存<=127
Short、Long缓存-128至127
Float、Dubbo没有缓存