public class Student{
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
}
class Demo{
list hs = new HashSet();
hs.add("hello");
hs.add(new Integer(10));
hs.add(new Student("dqf","23"));
hs.add2("hello");
hs.add2(new Integer(10));
hs.add2(new Student("dqf","23"));
//请问? 哪些不能执行成功?
}
如果想要知道答案,必须要知道hashset的相关知识,首先HashSet实现Set接口,set中的元素没有顺序,不可以重复,该集合中的元素不可以通过索引操作。那么HashSet到底通过什么方式来判断某个元素是否已经在集合中存在了呢?
静态常量池: 也就是class文件中的常量池
看下面一段程序:
public class test{
String items = "hello";
int item = 10 ;
Student student = new Student();
public void x(){...}
}
上面的这段java代码,在编译成.class文件时,除了会把,类的版本、字段、方法、接口等描述信息写入class文件中外,还会把一些特定的数据类型的字面值和符号引用加入到class文件中,这些字面值和符号引用就组成了class文件的常量池,即静态常量池。 例如在class文件中就会保存【“hello”、“10”】(字面值)、【“items”、“item”】(符号引用),字面值和符号引用之间没有关系。
那么到底哪些数据类型在java文件编译的时候会写入class文件的常量池呢?
据我的了解应该是八种基本数据类型和String类。
如果想要详细了解看着:
再看一段小程序:
String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);
int a = 3;
Integer b = new Integer(3);
System.out.println(a==b);
结果为:true
b自动拆箱成int类型再和a比较
String a = "hello";
String b = new String("hello");
System.out.println(a==b);//false
String类型没有自动装箱
String a = new String("hello");
String b = "hello"
System.out.println(a==b);//false
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);//true
System.out.println(f3 == f4);//false
对于 Integer f1 = 100;当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf。100不是自动转化成 new Integer(100); 而是自动转化成Integer.valueOf(100)
简单的说,如果整型字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,所以上面程序中f1==f2的结果是true,而f3==f4的结果是false。
Integer f5 = new Integer(10);
int f6 = 10;
System.out.println(f5==f6);//true
hs.add2("hello");//添加失败
想要知道为什么上面这一句会添加失败,首先要判断hs集合中到底有没有该对象,首先算出该对象的hashcode,如果在hs集合中能找到hashcode相等的对象,再比较两个对象equals()是否为true。
因为:String类型,重写了equals()和hashcode()方法,String的equals是比较两个对象的值,所以equals就为true;再更具我们上面的约定,equals相等,hashcode一定相等,所以添加失败。
hs.add2(new Integer(10));//添加失败,解释和hs.add2("hello")相同
①hs.add(new Student("dqf","23"));
②hs.add2(new Student("dqf","23"));//添加成功
这两句中创建的Student实例是完全不一样的,虽然这两个实例的hashcode可能相等,但是equals()必然不为true,因为Student中没有重写equals()方法。所以添加成功。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务