`
- 浏览:
153057 次
- 性别:
- 来自:
球面世界
-
java 代码
- public class HashCodeTest {
-
-
-
-
- @Before
- public void setUp() throws Exception {}
-
-
-
-
- @After
- public void tearDown() throws Exception {}
-
- @Test
- public void testHashCode(){
- String a = new String("AA");
- String b = a;
- String c = new String("AA");
- String d = new String("BB");
- String e = "AA";
- String f = "AA";
-
- System.out.println("String a hashcode is : "+ a.hashCode());
- System.out.println("String b hashcode is : "+ b.hashCode());
- System.out.println("String c hashcode is : "+ c.hashCode());
- System.out.println("String d hashcode is : "+ d.hashCode());
- System.out.println("String e hashcode is : "+ e.hashCode());
- System.out.println("String f hashcode is : "+ f.hashCode());
-
- System.out.println("a.equals(b) : "+ a.equals(b));
- System.out.println("a==b : "+ (a==b));
- System.out.println("a.equals(c) : "+ a.equals(c));
- System.out.println("a==c : "+ (a==c));
- System.out.println("b.equals(c) : "+ b.equals(c));
- System.out.println("b==c : "+ (b==c));
- System.out.println("a.equals(e) : "+ a.equals(e));
- System.out.println("a==e : "+ (a==e));
- System.out.println("e.equals(f) : "+ e.equals(f));
- System.out.println("e==f : "+ (e==f));
-
-
- Person p1 = new Person();
- Person p2 = p1;
- Person p3 = new Person();
-
- System.out.println("Model p1 hashcode is : "+ p1.hashCode());
- System.out.println("Model p2 hashcode is : "+ p2.hashCode());
- System.out.println("Model p3 hashcode is : "+ p3.hashCode());
- System.out.println("p1.equals(p2) : "+ p1.equals(p2));
- System.out.println("p1==p2 : "+ (p1==p2));
- System.out.println("p1.equals(p3) : "+ p1.equals(p3));
- System.out.println("p1==p3 : "+ (p1==p3));
- System.out.println("p2.equals(p3) : "+ p2.equals(p3));
- System.out.println("p2==p3 : "+ (p2==p3));
- }
-
- }
打印的结果为:
java 代码
- String a hashcode is : 2080
- String b hashcode is : 2080
- String c hashcode is : 2080
- String d hashcode is : 2112
- String e hashcode is : 2080
- String f hashcode is : 2080
- a.equals(b) : true
- a==b : true
- a.equals(c) : true
- a==c : false
- b.equals(c) : true
- b==c : false
- a.equals(e) : true
- a==e : false
- e.equals(f) : true
- e==f : true
- Model p1 hashcode is : 12934710
- Model p2 hashcode is : 12934710
- Model p3 hashcode is : 28064776
- p1.equals(p2) : true
- p1==p2 : true
- p1.equals(p3) : false
- p1==p3 : false
- p2.equals(p3) : false
- p2==p3 : false
结论:
待续。。。
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
第9章 常用类 4课时 理解Object类及其常用方法equals,hashCode和finalize等。 能够使用String,StringBuffer,StringBuilder类创建字符串对象和使用其方法,分辨不同类之间的区别。 ...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 ...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 ...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 5.5.2...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 ...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 ...
5.2.2相等测试与继承 5.2.3 hashCode方法 5.2.4 toString方法 5.3泛型数组列表 5.3.1访问数组列表元素 5.3.2类型化与原始数组列表的兼容性 5.4对象包装器与自动打包 5.5反射 5.5.1 Class类 ...
1.2.4 测试环境是否安装成功 8 1.2.5 如果失败了怎么办? 9 1.3 让自己的第一个程序运行起来 10 1.3.1 编写自己的Hello World源程序 10 1.3.2 编译自己的HelloWorld程序 11 1.3.3 让代码运行起来 13 1.4 初探...
1.2.4 测试环境是否安装成功 8 1.2.5 如果失败了怎么办? 9 1.3 让自己的第一个程序运行起来 10 1.3.1 编写自己的Hello World源程序 10 1.3.2 编译自己的HelloWorld程序 11 1.3.3 让代码运行起来 13 1.4 初探...
测试类的定义方式 57 简单的学生类编写示例 58 内存分析 59 属性(field,或者叫成员变量) 59 引用类型 60 类的方法 60 对象的创建和使用 60 构造器(或者叫做构造方法,constructor) 60 垃圾回收机制(Garbage ...
9.7.2 不允许进行测试的情况 174 9.8 小结 175 第10章 构造器——对象制造的工厂 176 10.1 基础知识 176 10.1.1 编写构造器的语法规则 176 10.1.2 访问限制修饰符与构造器 176 10.1.3 构造器与返回...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...
1,编写程序,判断给定的某个年份是否是闰年。 闰年的判断规则如下: (1)若某个年份能被4整除但不能被100整除,则是闰年。 (2)若某个年份能被400整除,则也是闰年。 import java.util.Scanner;...
1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得...
部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...
19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...