Programming languages generally have a set of operators that are similar to operators in mathematics.
An operator is a symbol that specifies which operation to perform in a statement or expression.
An operand is one of the inputs of an operator. For example, in expression b + c, + is the operator and b and c are the operands.
In many languages operators are handled by special subroutines. They often perform arithmetic, boolean and string operations. Relational operators can be used to test two values, for relationshipes such as equality or whether one value is greater than another.
Unlike subroutines, operators often provide the primitive operations of the language, their name consists of punctuation rather than alphanumeric characters, and they have irregular parameter passing conventions.
When an expression contains multiple operators, the precedence of the operators controls the order in which individual operators are evaluated. For example, in many languages expression b + c * d is evaluated as b + (c * d) because the * operator has higher precedence than the + operator: the * operator is evaluated first and the + operator second.
When an expression contains multiple operators with the same precedence, the associativity of the operators controls the order in which the operations are performed. If the operators are left-associative, operators are evaluated from left to right: the left operator is evaluated first and the right operator last; if they are right-associative, operators are evaluated from right to left.
In many languages precedence and associativity can be controlled using parentheses (). In the precedence example above, expression b + c * d is evaluated as b + (c * d), where the * operator is evaluated first and the + operator second. Writing the expression as ( b + c ) * d causes the + operator to be evaluated first and the * operator second.
An assignment operator assigns the value of an expression to a variable.
Assignment can be either by value or by reference.
Assignment by value copies the actual value of the expression and stores it in the variable. Assignment by value is used when a variable is assigned a number or string literal (i.e. representation of the string value within the source code).
Assignment by reference stores a reference to the expression in the variable. Assignment by reference is commonly used with the new operator. Use of the new operator creates an object in memory and a reference to that location in memory is assigned to the variable.
In C++, operators follow a strict precedence, which defines the evaluation order of expressions containing these operators unless explicitly forced by parentheses. Operators with the same precedence associate with either the expression on their left or the expression on their right, depending on their associativity. The following table shows the precedence and associativity of C++ operators (from highest to lowest precedence).
Precedence | Operator | Description | Associativity |
---|---|---|---|
scope resolution | :: | scope resolution | left to right |
primary | ++ | postfix increment | left to right |
-- | postfix decrement | ||
() | function call | ||
[] | array subscript | ||
. | member seletion by reference | ||
-> | member selection through pointer | ||
typeid() | run-time type information | ||
const_cast | type cast | ||
dynamic_cast | type cast | ||
reinterpret_cast | type cast | ||
static_cast | type cast | ||
unary | ++ | prefix increment | left to right |
-- | prefix decrement | ||
+ | unary plus | ||
- | unary minus (two's complement) | ||
~ | complement (one's complement) | ||
! | logical not | ||
( type ) | type cast | ||
* | indirection, dereference | ||
& | reference | ||
sizeof | size-of | ||
new | dynamic memory allocation | ||
delete | dynamic memory deallocation | ||
pointer | . | member | left to right |
-> | indirect member | ||
multiplicative | * | multiplication | left to right |
/ | division | ||
% | modulus | ||
additive | + | addition | left to right |
- | subtraction | ||
shift | << | left shift | left to right |
>> | right shift | ||
relational | < | less than | left to right |
> | greater than | ||
<= | less than or equal | ||
>= | greater than or equal | ||
equality | == | equality | left to right |
!= | inequality | ||
and | & | and | left to right |
exclusive-or | ^ | xor (exclusive-or) | left to right |
or | | | or (inclusive-or) | left to right |
conditional and | && | conditional and | left to right |
conditional or | || | conditional or | left to right |
conditional | ? : | ternary conditional | right to left |
assignment | = | assignment | right to left |
+= | assignment by sum | ||
-= | assignment by difference | ||
*= | assignment by product | ||
/= | assignment by dividend | ||
%= | assignment by remainder | ||
<<= | assignment by left-shift | ||
>>= | assignment by right-shift | ||
&= | assignment by and | ||
^= | assignment by xor | ||
|= | assignment by or | ||
exception | throw | throw exception | |
comma | , | comma | left to right |
In Java, operators follow a strict precedence, which defines the evaluation order of expressions containing these operators unless explicitly forced by parentheses. Operators with the same precedence associate with either the expression on their left or the expression on their right, depending on their associativity. The following table shows the precedence and associativity of Java operators (from highest to lowest precedence).
Precedence | Operator | Description | Associativity |
---|---|---|---|
primary | ++ | postfix increment | |
-- | postfix decrement | ||
unary | ++ | prefix increment | |
-- | prefix decrement | ||
+ | unary plus | ||
- | unary minus (two's complement) | ||
~ | complement (one's complement) | ||
! | logical not | ||
multiplicative | * | multiplication | left to right |
/ | division | ||
% | modulus | ||
additive | + | addition | left to right |
- | subtraction | ||
shift | << | left shift | left to right |
>> | signed right shift | ||
>>> | unsigned right shift | ||
relational | < | less than | left to right |
> | greater than | ||
<= | less than or equal | ||
>= | greater than or equal | ||
instanceof | is specified type | ||
equality | == | equality | left to right |
!= | inequality | ||
and | & | and | left to right |
exclusive-or | ^ | xor (exclusive-or) | left to right |
or | | | or (inclusive-or) | left to right |
conditional and | && | conditional and | left to right |
conditional or | || | conditional or | left to right |
conditional | ? : | ternary conditional | |
assignment | = | assignment | right to left |
+= | assignment by sum | ||
-= | assignment by difference | ||
*= | assignment by product | ||
/= | assignment by dividend | ||
%= | assignment by remainder | ||
<<= | assignment by left-shift | ||
>>= | assignment by signed right-shift | ||
>>>= | assignment by unsigned right-shift | ||
&= | assignment by and | ||
^= | assignment by xor | ||
|= | assignment by or |
examples | GCC C++ |
Borland C++ Compiler | |
Java | |
home | Home Page |