- Typeof
Typeof (alternately typeof or TypeOf) is an operator provided by several
programming language s which determines thedata type of a givenvariable . This can be useful when constructing parts of programs that need to accept many types of data but may need to take different action depending on the type of data provided.In languages that support
polymorphism andtype casting , the typeof operator can have one of two distinct meanings when applied to an object. In some languages, such asVisual Basic , [http://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx "TypeOf Operator (Visual Basic)" inMSDN ] the typeof operator returns thedynamic type of the object. That is, it tells the program what the true, original type of the object is, irrespective of any type casting. In these languages, the typeof operator is the method for obtainingrun-time type information .In other languages, such as C# [http://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx "typeof (C#)" in MSDN] and some nonstandard extensions to C and
C++ http://gcc.gnu.org/onlinedocs/gcc/Typeof.html "Typeof" in Using the GNU Compiler Collection] , the typeof operator returns thestatic type of the object. That is, it tells the program what type the object is declared to be at that point in the program, irrespective of its original form. These languages usually have other constructs for obtaining run-time type information.In Java, the feature is implemented via instanceof.
Example
* In C language, the GCC extension provides typeof (copied from ).
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
* in Java language
// Given a wrapped object, return the JNI type signature for the corresponding primitive value.public static String getJNISign(Object o) { if (o instanceof Integer) return "I"; else if (o instanceof Byte) return "B"; .... else return o.getClass().getName();}See also
*
Run-time type information References
Wikimedia Foundation. 2010.