---
title: "Effective java 对像引用和hashcode和equals方法实现"
author: "rainyear"
date: "2011-10-10T10:12:19Z"
tags: []
categories: ["非技术"]
slug: "effective-java-hashcode-equals"
draft: false
lastmod: "2011-10-10T10:12:19Z"
id: 1188372
url: "https://rainyear.iteye.com/blog/1188372"
---

1:是考虑用static 来代替构造方法，通过私有构造器强化不可实例化的能力
public class test{
pirvate  test(){
throw new AssertionError();
}
}
一般可用在工具类里面一般可都是static,
2:避免创建不必要的对像
String s=new String("sql");
改进后的版本可以：String s="sql";
重写：
public boolean equals (Object o){
if(o==this){
return true;
}if(!(0 instanceof Test)){
return false;
}
Test ts=(Test)o;
return ts.id=id&&ts.name=name;
}

pubilc int hashcode(){
int result=17;
result=31*result+id //如果是int类型直接int，如果是long .intval(),如果是float 计算Float.floatToIntBits(id),如果是double直接转换成：Double.doubleToLongbits(id);String 直接化成int类型。
boolean 计算（f?1:0）

}