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 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没有缓存
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- Equals和==的区别 公共变量和属性的区别小结
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码