您的位置:首页 > 其它

Integer和String底层原理

2015-11-09 16:14 344 查看
 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方法,在小项目中后端可以用来加密金额之类的,在前端再次使用即可以反转回来

 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比较值

 Boolean缓存全部,即true,false

Byte缓存全部,即-128至127

Characte缓存<=127

Short、Long缓存-128至127

Float、Dubbo没有缓存 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Integer String equals 等于