我正在寫一個類Vec2D
,表示一個2維向量。我在double
中儲存了x
和y
。Double.doubleToLongBits(x)的含義
當記者問到產生equals(Object obj
和hashCode()
,日食產生這樣的:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp^(temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp^(temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vec2D other = (Vec2D) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
return true;
}
什麼是Double.doubleToLongBits(x)
在這方面的意義是什麼?我能不能簡單地寫x != other.x
?