Forgot your password?  

Not What You Meant?  There are 8 definitions for Left.  Also try: Assignment.

Assignment Operator | Research & Encyclopedia Articles

Print-Friendly   Order the PDF version   Order the RTF version
About 2 pages (442 words)
Assignment (computer science) Summary

 


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).

More Information
  • View Assignment Operator Study Pack
  • 8 Alternative Definitions
  • Search Results for "Assignment Operator"
  • More Products on This Subject
    Assignment Statement
    The assignment statement is the simplest expression in computer programming. Most programming langu... more

    Compound Assignment
    The compound assignment is a form of assignment statement used to perform multiple operations on va... more


    Ask any question on Assignment (computer science) and get it answered FAST!
    Answer questions in BookRags Q&A and earn points toward
    discounted or even FREE Study Guides and other BookRags products!
    Learn more about BookRags Q&A
    Copyrights
    Assignment Operator from World of Computer Science. ©2005-2006 Thomson Gale, a part of the Thomson Corporation. All rights reserved.

    Join BookRagslearn moreJoin BookRags

    Join BookRagslearn moreJoin BookRags