Java code riddle Operators
Starting from the simplest operator plus sign (+), the plus sign (+) is a binary operator -- that is, the plus sign only connects two numbers, never link the third or more.
Therefore, "1, 2, and 3" is expressed:
(1 + 2) + 3 // a or
1 + (2 + 3) // although we usually write 1 + 2 + 3, it does not mean that it is equivalent to 1 + 2 + 3 in our mathematics.
So in mathematics, 1 + 2 + 3 represents a or B?
If the computer's evaluation value is left-bound, this expression is equivalent to the first a. If it is right-bound, this expression is equivalent to the second B.
The simple understanding of 1 + 2 + 3 is to "Add 1, 2, and 3 together". Indeed, in the mathematics we come into contact, we add up the three numbers. But in programming languages, this is not just the case.
As mentioned above, the plus sign cannot operate three or more numbers. Only two numbers can be involved in addition operations.
By the way, positive and negative signs are unary operators. Although they use the same symbols as binary operators, they are different, therefore, do not assume that + 4 is equivalent to 0 + 4. In fact, they are not equivalent,
+ 4 is an integer, but 0 + 4 is an addition expression. The result of this expression is exactly + 4.
In java, we can write short a = + 4, but a warning is generated when we write short a = 0 + 4.
There is another example about short,
Short B = 1;
Short B = B + 4; // warning
Short B + = 4; // if there is no warning, how does 1 + 2 + 3 work? In the programming language of the Von noriman architecture, there is a side effect here-I'm used to saying that when the computing process of a computer is different from that of a programmer's brain, this is called a side effect. "(although it has never been written in books, I always think so.) You thought it would be like this, but the computer did not, I call him a side effect.
If you have read the preceding statements and expressions, you can understand them as follows:
1 + 2 is an expression, and its return value is 3. The return value of this expression is 3 + 3 in another expression, and result 6 is obtained.
We use the Statement to rewrite this Code:
// Calculate 1 + 2 + 3
Var a = 1 + 2;
Var B = a + 3; if we evaluate this expression using the lisp language, there is no side effect.
(+ 1 2) 3) if you haven't understood it yet, or this example is too special, let's change
5> 4> 3 in mathematics, the value of this formula is true. When we write this code in C language, it returns false.
The reason is the same as above. Greater than (>) is a binary operation, which cannot directly compare three numbers. The result returned by 5> 4 is true. When true and 3 are used for comparison, true is converted to 1, that is, 1> 3. The final result is false.
In short, return to the view in "statements and expressions": In programming languages, each expression has a value.
Operators in programming languages are the same as those in mathematics, but they are not the same. When you write a program, you must write it to others. When you debug the program, you must learn to think about the meaning of the Code in a computer.
I often think of operators as functions. For example, 2 + 5 is actually add (2, 5) or 2.add( 5 ). Do I secretly tell you, "In fact, many languages do this ".