Java knowledge learned by 16.10.18

Source: Internet
Author: User
Tags bitwise knowledge base

1.

Suddenly I find myself forgetting what is the difference between a self-increment operator and a variable?

So I checked the information.

If you only add 1 or minus 1 to the argument, there is no difference between the front and the back.

However, if the self-increment decrement operator is used when it is necessary to assign values to other variables or to be evaluated with other expressions, it is important to note that since this time the self-increment decrement operator is placed in front and back there is a difference.

It is obvious to see that when the assignment is performed, when the operator is placed in the variable, the variable is taken first, then the variable is added one or minus one, and when the operator is placed before the variable, the variable is first added one minus one, and then the value of the variable is taken.

Now, after such a search, it immediately deepened the impression on this issue.

2.

Use the most efficient method to calculate the 2*16 equals a few.

Based on my shallow knowledge base, the first to see this problem, I really do not understand, obviously directly multiply not on it, there are other operations in Java can calculate 2*16,.

So after I read the information, I learned that:

In a computer, bit arithmetic is the most efficient. Therefore, in order to find the most efficient method, we should start with the bitwise operation.

The bitwise operation of Java provides left shift ' << ', right Shift ' >> ' and unsigned Right shift ' >>> ' operator, A<<b, which is a Times 2 B. So 2*16 wrote a 2<<4;-bit operation to perform binary shift operations directly on the data, and the multiplication operation should be converted to the computer first and then calculated to get the correct result.

In summary, the use of 2<<4-bit operation is the most efficient.

3.

In order to prove the correctness of the second question, I want to know the time when a program is running, so I go online and check it.

It is understood that:

"At the beginning of the program there is a variable record start time, for example, start = System.currenttimemillis (), and the last line with a variable to record the end time, for example, end = System.currenttimemillis (), Subtract both, and the result is in milliseconds.

System.nanotime (); It seems that the accuracy is higher. The actual test System.currenttimemillis () accuracy is 16ms. Nanotime is 1-2ms ", with this method, you can get the program to run the time, of course, I found a problem, that is, the program every time the operation is different, I guess it may be due to hardware or what special cause.

4.

& is bitwise AND Operation,&& is a logical operator

5.

This is obviously true in C + +, but, in Java, this is not a successful compilation. So, why?

The default integer type in Java is int, and the result is always converted to the highest precision data type for different types of numeric values, so the accuracy of this variable should not be lower than the data type with the highest precision of the participating calculation, or a compilation error will occur if the calculation result is assigned to a variable.

Java knowledge learned by 16.10.18

Related Article

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.