java对象比较
2024-09-25 01:04:54
public class InternDemo {
public static void main(String[] args){
/*
jdk7版本之后 字符串常量池从Perm Space移到Java Heap
*/
//生成两个对象
String s1 = new String("aaa");
//直接使用双引号声明出来的String对象会直接存储在常量池
String s2 = "aaa";
//非双引号声明的string对象,可以使用intern方法,若字符串存在,使用查询的字符串,若不存在将当前字符串放入常量池
s1.intern();
//比较堆内存地址
System.out.println(s1.equals(s2));
//比较引用地址
System.out.println(s1 == s2);
//equals相等,hashcode一定相等
System.out.println(s1.hashCode() == s2.hashCode()); //生成两个对象
String s3 = new String("") + new String("");
String s4 = "";
s3.intern();
System.out.println(s3.equals(s4));
System.out.println(s3 == s4);
System.out.println(s3.hashCode() == s4.hashCode());
/*
true
false
true
*/
}
}
最新文章
- 阿里云CentOS7系列三 -- 配置防火墙
- 关于内存管理/set/get方法
- HDU4276 The Ghost Blows Light SPFA&;&;树dp
- jsp获取枚举的值
- linux添加用户
- 【百度地图API】北京周边7日游——图标按路线轨迹行动
- jQuery基础---Ajax进阶
- Java面向对象回顾(1)
- halcon c++ 异常处理
- Solr中的group与facet的区别
- paloalto防火墙内存使用率高
- pair在vector和map中的使用
- Nginx下配置网站ssl实现https访问
- 7种方法实现移动端Retina屏幕1px边框效果
- PythonStudy——数据类型转化 Data type conversion
- centos7系统下hostname解析
- vscode sync插件 在不同设备 同步的坑
- Android 微信支付资料收集
- HTML5、CSS3与响应式Web设计入门(2)
- nginx和php-fpm通信的两种方式 unix socket和TCP