Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?

Let's look at a very simple Java code,
Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?
the code is very simple, no technical content. But if we put this code into the following way
Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?
everyone we can see that we only use a variable instead of the original assignment expression among the constant 1, a syntax error occurs, which is Why? Today, we use a short article to talk about this topic.
We know that, Java language has four integer types, namely, byte, short, int, and long. Which, Java compiler for byte and short types of variables at the time of assignment, do a little "special inspection." Then the compiler how to "special inspection" of these two types of variables? When the compiler sees an assignment for these two types of variables of time, to be "beyond the scope of examination", that is to say, will be to check the value of the variable being assigned will not be likely to exceed the range. If the compiler think that the assigned value may exceed the maximum or minimum value of this variable can be stored, it will report a syntax error. But many people will ask the program to the variable s value assigned does not exceed the range, why is it being given?
We will talk about checking mechanism compiler. When the compiler sees the program is not variable s assignment with a simple value, but to an arithmetic expression assigned to the s, and arithmetic expression also appeared variable. This time the compiler will think that this assignment would likely have a range of super value assigned to s, so on the error.
Readers may ask: paragraph 1 of the code which is also assigned to the variable s arithmetic expressions, why will not appear grammar is wrong with it? The problem is: in paragraph 2 of the program, to the variable arithmetic expressions s assignment appeared variable. The compiler believes that since it is "variable", it may change, it is an uncertain factor. The compiler and never mind the current value of the variable in the end is how much, as long as it is involved in the calculation variables, variable values may change, which may result in the assignment of range, and therefore reported a syntax error.
If we put in paragraph 2 of the variables in a program preceded by a final keyword will happen then? Consider the following code
Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?
When the final preceded by a keyword, a value can no longer change, it becomes a constant. The compiler will identify the assignment is safe because the value of a is always 1, certainly not exceed the scope of the assignment.
Well, is not assigned to the arithmetic s table when the variable does not appear, the assignment will certainly not being given it? But it is not, consider the following example of
Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?
this assignment, "=" arithmetic expression on the right and there are no variables appear, but will still report a syntax error, the reason is that the compiler will advance to calculate the value of an arithmetic expression out, if it is found from the calculated data has exceeded the scope of byte or short, will report an error. Paragraph 4 therefore can not compile the program.
Here, we can remember two conclusions:
1. assignment to a variable byte or short, "=" If the right is an arithmetic expression, variables and expressions appear, certainly will not compile.
2. that the use of a byte or short constant variable assignment, if the "compilation phase" will be able to determine the assigned value has exceeded the range, the same error.
Furthermore, we need to remind you that the time for a variable of type int and long assignment, the compiler does not use such special screening measures.
Asked one thousand Java: Java language will complain why the assignment is short and byte type variables?
We can see in the above program, we give a variable of type int i1 assignment, the "=" on the right is an arithmetic expression, and the expression is also variable, but does not appear syntax errors. When performing assigned to i2, "=" right value has exceeded the scope of type int, there will not be a problem.
By this short article, I believe the reason when the junior partner will be able to figure out why and short byte variable assignments to be wrong.
If you want to learn Java programming system, you can click here to watch video lessons, there is a problem you can also add my QQ group to discuss 291,839,907!

Guess you like

Origin blog.51cto.com/2266836/2460902