Operator (computer programming): Difference between revisions

Content deleted Content added
Cookman (talk | contribs)
No edit summary
m simple spelling corrections
Line 7:
In some programming languages an operator may work with more than one kind of data, (such as in [[Java programming language|Java]] where the <tt>+</tt> operator is used both for the addition of numbers and for the concatenation of strings). Such an operator is said to be ''overloaded''. In languages that support [[operator overloading]] by the programmer, such as [[C Plus Plus|C++]], one can define customized uses for operators; in [[Prolog]], one can also define new operators.
 
Some languages also allow for the operands of an operator to be of different data types, in which case one of them is ''coerced'' to the data type of the other, to permit the operation to occur. For example, in [[Perl]] <tt>12 + "3.14"</tt>, causes the text <tt>"3.14"</tt> to be coerced to a number and produces the result <tt>15.14</tt>. The text <tt>"3.14"</tt> must be transformed into the value 3.14 befor addition can take place. Also 12 is an integer and 3.14 is either a floating or fixed point number (a number the has a decimal place in it) Therefor the integer must be then converted to a floating point or fixed point number. This is however dangerousedangerous in most languages as changing the typing of youyour variables during execution can have undesired effects and so it is often important in most high level languages that strict typing rules are kept.
 
In older languages like C. The union was used
Line 18:
</code>
 
to allow loseloose typing. Defining it like this you are always aware of what the possible date you are dealing with is. It is true to say that loseloose typing should be used sparingsparingly if possible. And you should be aware of the type of date retured from your operator and what the ''coerced'' effects are in the language you are using.
 
{{comp-stub}}