Assignment Operator
The assignment operator in a computer language takes the value of an expression and copies it to a variable. In most languages it is denoted by the familiar mathematical symbol "=", but in some languages, like Pascal, it is denoted by the symbol ":=", which roughly means "becomes equal to." To distinguish "assignment" from "equivalence" (which in mathematics both use "="), most programming languages use the symbol "==". So "a = b" makes "a" take on the value of "b"; "a == b" checks to see if "a" and "b" are equal.
In general the semantics of the assignment operator is: modifiable object = expression. The value of the expression is placed in the modifiable object's data area. A typical example of valid assignment statements in C, C++, and Java is:
- int myInt = 3;
- int myOtherInt = myInt;
The second form of assignment operator is the "compound assignment," which takes the form: modifiable object <operator> = expression.
In this example the value of the expression and the original object are placed in the storage area of the original object after being operated upon by <operator>. It is exactly equivalent to writing: modifiable object = modifiable object <operator> expression. A typical example of valid compound assignment statements in C, C++, and Java is
- int myInt = 3;
- myInt += 2;
In this case, myInt is originally given the value of 3. The compound assignment operator then applies <operator> in this case "+" to the two values, in this case myInt and 2, and places the result, 2 + 3 = 5, in myInt. This is just the same as writing: myInt = myInt + 2;.
C, Java, and C++ allow most of their operators to be used with compound assignment syntax. For example, += (addition), -= (subtraction), *= (multiplication), and /= (division) are all allowed by all three languages (and a good many more).
C++ goes further by allowing "operator overloading," which means that the assignment operator for a given type of object, or class, can be arbitrarily redefined to give the behaviour desired. This allows the programmer to make a copy of an arbitrarily complex object with a simple assignment statement. The assignment still invokes a potentially complex function, but this is hidden by the syntactic sugar of the operator overloading. It is of course incumbent on the programmer not to do the unexpected when overriding an operator (for example redefining addition as subtraction would be a bad move).
C and Java do not support operator overloading, though Java has a single exception with its built-in String class. Even then it is not possible to alter the overloading of the String class's operators as it is part of the language definition.
This is the complete article, containing 442 words
(approx. 1 page at 300 words per page).