Intro

Operators join smaller expressions to form larger expressions. Just as a function performs some function with its arguments (if any), operators operate on operands (if any). However while functions have some nice syntax like printf("hello"), operators have their own syntax.

  • Operators usually work with zero, one, two, or three operands, i.e. operators are nilary, unary, binary, or ternary.
  • Operators in the Cs can be overloaded by the developer, i.e. they may operate differently depending upon the syntax or the datatype of the operand(s). This is a type of ad hoc polymorphism. Exceptions in the table below are marked with "No overloading".
    • Java does not allow operator overloading. The only overloaded operator is +, which is also used to concatenate strings.
  • The Cs do not allow coercion (where operators or functions implicitly convert datatypes).
    • EG: 1 + "2" does not yield 3, but is instead a syntactical error.
    • JavaScript does allow coercion.
  • Operator association is usually left-to-right. Exceptions in the table below are marked with "(R-to-L)".
  • The operators of the Cs usually use infix notation (operator between operands. EG: a+b), but may use prefix notation  or postfix notation.
  • Operators have precedence. EG: 1+2*3 is the same as 1+(2*3) because the operator * has a higher precedence than the + operator.
  • Annoyingly, some keywords are also operators in some languages but not in others.

Here are the operators in order of precedence (highest to low).

Precedence Example Description C C++ Java C# JavaScript
Scope ::NUM_ELEMENTS Unary scope resolution for globals. n y      
std::cout Binary scope resolution for class and namespace members. n y      
Postfix swap(x,y) Function call. y y y y y
arr[7] Array subscript/index y y y y y
object.member Element selection or member access, identifier. For object oriented programming. No overloading. y y y y y
ptr->member equivalent to (*ptr).member Element selection or member access, pointer y y     n
typeid(std::cout) Run time type ID for an object. n y      
typeid(std::iostream) Run time type ID for a type. n y      
static_cast<float>(i) Compile-time type conversion, cast operator. n y      
dynamic_cast<std::istream>(stream) RTTI type conversion, cast operator. n y      
const_cast<char*>("Hello") Removes the const qualifier from a pointer or a reference, cast operator. n y      
reinterpret_cast<const long*>("Hello") Interprets raw data as being of some other type, cast operator. n y      
int(myFloat) Functional cast operator. n y   y  
std::string("Hello",0,5) Constructor call for creating a temporary object, esp. of a class type n y      
(R-to-L)
Unary
b=++a is the same as a=a+1; b=a;. Unary increment before use.
y y y y y
b=--a is the same as a=a-1; b=a;. Unary decrement before use. y y y y y
b=a++ is the same as b=a; a=a+1;. Unary increment after use. y y y y y
b=a-- is the same as b=a; a=a-1;. Unary decrement after use. y y y y y
+i Unary plus. y y y y y
-i Unary minus. Changes the sign. y y y y y
!isTrue yields false. Logical NOT. y y y y y
~mask Bitwise NOT or zero's complement y y y y y
i = (int)f Cast. Converts the variable to the datatype in parentheses. DEPRECATED IN C++. y y y y  
i = *pi Dereference. "pointer". Gets the value of a reference. y y      
int *pi = &i Reference. "address of". Makes a reference/pointer. y y      
sizeof 123 Size. Gets the size in bytes of expressions. No overloading. y y      
sizeof(int) Size. Gets the size in bytes of types. No overloading. y y      
new int New, instantiates object, single. n y y y  
new int[9] New, instantiates object, dynamically allocated array. n y      
new (arg1) int New, instantiates object, single, with arguments. n y      
new (arg1) int[9] New, instantiates object, dynamically allocated array, with arguments. n y      
delete ptr Delete, frees memory of pointer. n y      
delete [] arr Delete, frees memory of dynamically allocated array. n y      
(R-to-L)
Member pointer
obj.*memptr Member pointer of an object of class/union type or a referemce to it. n y      
ptr->*memptr Member pointer of a pointer to an object of class/union type. n y      
Multiplicative 5*2 yields 10. Multiplication. y y y y y
5/2 yields 2.5. Division. y y y y y
5%2 yields 1. Modulus gives the remainder of a division of two integer values. y y y y y
Additive 5+2 yields 7. Addition. y y y y y
5-2 yields 3. Subtraction.   y y y y
Bitwise shift 9<<2 yields 36 since 1001 becomes 100100. Bitwise left shift. Shift binary a to the left b places and pad the right side with b 0s. y y y y y
9>>2 yields 2 because 1001 becomes 10. Bitwise right shift. Shift binary a to the right b places and discard the b rightmost digits. y y y y y
9>>>2 yields 2 because 1001 becomes 10. Bitwise right shift and pad the left with b 0s. Shift binary a to the right b places and discard the b rightmost digits then pad the left with 0s. For non-negatives, >> and >>> yield the same results. n n y n y
Relational inequality a<a yields false. Relational less than. Returns true if true. y y y y y
a<=a yields true. Relational less than or equal to. Returns true if true. y y y y y
a>a yields false Relational greater than. Returns true if true. y y y y y
a>=a yields true Relational greater than or equal to. Returns true if true. y y y y y
Relational equality a==a yields true Comparison equal to. y y y y y
a!=a yields false Comparison not equal to. y y y y y
  10&12 yields 8, since 1010 and 1100 yields 1000. Bitwise AND. Returns 1s for bit positions where both operands are 1. y y y y y
  10^12 yields 6, since 1010 and 1100 yields 0110. Bitwise XOR (exclusive or). Returns 1s for bit positions where either operand is 1 but not both. y y y y y
  10|12 yields 14, since 1010 and 1100 yields 1110. Bitwise OR (inclusive or). Returns 1s for bit positions where either operand is 1. y y y y y
  a&&b Logical AND y y y y y
  a||b Logical OR y y y y y
(R-to-L) (a>b)?a:b yields the greater of a or b. Ternary conditional. Given c?t:f, if c is true, then return t, else return f. No overloading. y y y y y
(R-to-L)
Assignment
a=b
Direct assignment. EG: a = 1 + (b=2). EG: a=b=c=3. y y y y y
a+=b is the same as a = a+b.

Additive assignment.

y y y y y
a-=b is the same as a = a-b. Subtractive assignment. y y y y y
a*=b is the same as a = a*b. Multiplicative assignment. y y y y y
a/=b is the same as a = a/b. Divisive assignment. y y y y y
a%=b is the same as a = a%b. Modulus assignment. y y y y y
a<<=b is the same as a = a<<b. Bitwise shift assignment y y y y y
a>>=b is the same as a = a>>b. Bitwise shift assignment y y y y y
a>>>=b is the same as a = a>>>b. Padding bitwise shift assignment n n y n y
a&=b is the same as a = a&b. Bitwise AND assignment y y y y y
a^=b is the same as a = a^b. Bitwise XOR assignment y y y y y
a|=b is the same as a = a|b. Bitwise OR assignment y y y y y
  throw "index out of bounds" Throw exception n y      
  while(x=func(1),y+=3,z<9)
for (var i=0, j=9; i <= 9; i++, j--)
document.writeln("a["+i+","+j+"]= " + a[i,j])
Comma operator. Evaluate a list of expressions but return the last one. y y   y y

Notes by Language

Java

Additional Java operators:

  • var1 instanceof var2
  • +. Note that the plus sign is used as both an arithmetic and a string operator. EGs:
    8 + 8        // 16
    "8" + 8      // "88"
    8 + "8"      // "88"
    "8" + "8"    // "88"
    8 + 8 + "8"  // "168"
    8 + "8" + 8  // "888"
    

C#

Additional C# operators:

  • as type.
  • object is Class.
  • true. Usually a literal though.
  • false. Usually a literal though.
  • typeof(type).
  • checked(expression).
  • unchecked(expression).
  • type * ptr = stackalloc type [ expr ].

JavaScript

  • Comparison notes:
    • In comparisons, NaN is not equal to anything, including NaN. Null and Undefined are equal.
    • a==b. True if operand values are equal. A data type conversion is performed if needed. EG: 5=="5" returns true. Objects must have same address space. EG: a1=new a; a2=new a; thus a1==a2 returns false.
    • ===. Strict equal. True if operand values and data types are equal. EG: 5==="5" returns false. Objects must have same address space. EG: a1=new a; a2=new a; thus a1===a2 returns false.
    • !==. Strict not equal.
  • + and +=. Note that the plus sign is used as both an arithmetic and a string operator. EGs:
    8 + 8        // 16
    "8" + 8      // "88"
    8 + "8"      // "88"
    "8" + "8"    // "88"
    8 + 8 + "8"  // "168"
    8 + "8" + 8  // "888"
    strResult += "WHERE x = 1"
  • delete objectName
    delete objectName.property
    delete objectName[index]
    delete property  //use within a with statement
    . Deletes an object, an object's property, or an element at a specified index in an array.
  • propertyNameOrNumber in objectName. Returns true if the specified property is in the specified object.
  • objectName instanceof objectType. Returns true if the specified object is of the specified object type.
  • objectName = new objectType (param1 [,param2] ...[,paramN]). Creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
  • this. Refers to the current object, usually the calling object. EG: onChange="run(this, 12)".
  • typeof expression
    typeof (expression)
    . Returns string indicating data type. The strings returned are: string, number, Boolean, object, function, undefined, or null.
  • void (expression)
    void expression
    . Returns no value at all. EG: <a href="void(document.form.submit())">Click here to submit</a>.


GeorgeHernandez.comSome rights reserved