Java code riddle operator

  • 2020-04-01 01:10:30
  • OfStack

Starting with the simplest operator, the plus sign (+) is a binary operation-that is, the plus sign joins only two Numbers and never a third or more.
Thus, "1 + 2 + 3" is expressed in the computer as:
1 plus 2 plus 3 over a or
1+ (2 +3) // b although we usually write it as 1+2+3, that doesn't mean it's the same thing as 1+2+3 in our math.
So is 1+2+3 a or b?
If the evaluation of the computer is left bound, then this expression is equivalent to the first kind of a; If this is the right bound, then this expression is equivalent to the second b.
1 + 2 + 3 is simply "add 1, 2, and 3 together," and indeed, in the mathematics that we've been exposed to, it's just adding up three Numbers. But in programming languages, that's not all.
As mentioned earlier, the + sign cannot operate on three or more Numbers, and only two Numbers are involved in addition.
By the way, plus and minus signs are unary operators, and although they have the same notation as binary operators plus and minus, they are different, so don't assume that plus 4 is the same thing as 0 plus 4, they're not,
+4 is an integer, but 0+4 is an additive expression that evaluates to exactly +4.
In Java, we can write short a = +4, but it generates a warning when we write short a = 0 +4.
There's another example, again, of short,
Short b = 1;
Short b is equal to b plus 4; / / warning
Short + b = 4; // no warning so how does 1 + 2 + 3 work? In the von neumann architecture according to the man of programming languages, there is a side effect - I used to say that "brain thinking process and operation process of computer programmer is not the same, it is called a side effect" (although not so written in books, but I always think so), would you think that's the case, the results of computer just didn't do that, I called him the side effects.
If you've seen the previous statement and expression, here's how it works:
1 + 2 is an expression that returns 3. The return value of this expression is then added to another expression 3 + 3, resulting in a result of 6.
Let's rewrite this code with a Statement:
// calculate 1 + 2 + 3
So var a is equal to 1 plus 2;
Var b is equal to a plus 3; If we evaluate this expression in lisp, there are no side effects.
(+ (+ 1, 2,) 3) if you don't get it, or if this example is too specific, let's switch
5 > 4 > In mathematics, the value of this expression is true. When we write this code in C, it returns exactly false.
The reason is the same as above, greater than sign (>). It's a binary operation, it can't directly compare three Numbers, 5 > 4 returns true, and when compared with 3, true is converted to 1, which is 1 > 3, the end result is false.
Anyway, going back to the point of "statements and expressions" : in a programming language, every expression has a value.
The operators in programming languages are the same as the operations in mathematics, but they are not the same. When you write a program, write it for people; When you debug a program, learn to think about the meaning of the code in a computer way.
I'm used to thinking of operators as functions, like 2 + 5 is actually add(2, 5) or 2.add(5). Would I secretly tell you that "many languages do this"?

Related articles: