For:
2 << 3 (shift left 3 is the equivalent of multiplying by 2 by 3, and right 3 is equivalent to 2 by 3).
Add: When we rewrite the Hashcode method for a class we write, we may see code like the following, in fact we do not understand why we use such multiplication to generate hash code (hash code), and why this number is a prime, why usually choose 31 this number? The answer to the first two questions you can Baidu, choose 31 is because you can use SHIFT and subtraction operation instead of multiplication, so as to get better performance. You may have thought about it here: * num is equivalent to (NUM << 5)-Num, the left 5 is the equivalent of multiplying by 2 by 5 and subtracting itself by the equivalent of multiplying by 31, now the VM can automatically complete this optimization.
public class PhoneNumber {
private int areacode;
Private String prefix;
Private String linenumber;
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + AreaCode;
result = Prime * result
+ ((linenumber = = null)? 0:linenumber.hashcode ());
result = Prime * result + ((prefix = = null)? 0:prefix.hashcode ());
return result;
}
@Override
public boolean equals (Object obj) {
if (this = = obj)
return true;
if (obj = = null)
return false;
if (GetClass ()! = Obj.getclass ())
return false;
PhoneNumber other = (phonenumber) obj;
if (areacode! = Other.areacode)
return false;
if (linenumber = = null) {
if (other.linenumber! = null)
return false;
} else if (!linenumber.equals (Other.linenumber))
return false;
if (prefix = = null) {
if (other.prefix! = null)
return false;
} else if (!prefix.equals (Other.prefix))
return false;
return true;
}
}
Calculate 2 times 8 with the most efficient method?