Calculate 2 times 8 with the most efficient method?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.