Typeof

Typeof

Typeof (alternately typeof or TypeOf) is an operator provided by several programming languages which determines the data type of a given variable. 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 and type casting, the typeof operator can have one of two distinct meanings when applied to an object. In some languages, such as Visual Basic, [http://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx "TypeOf Operator (Visual Basic)" in MSDN] the typeof operator returns the dynamic 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 obtaining run-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 the static 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.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • decltype — In the C++ programming language, decltype is an operator for querying the type of an expression. It was introduced in the current version of the C++ standard, C++11. Its primary intended use is in generic programming, where it is often difficult …   Wikipedia

  • JavaScript syntax — This article is part of the JavaScript series. JavaScript JavaScript syntax JavaScript topics This box: view · …   Wikipedia

  • ECMAScript — Класс языка: мультипарадигменный: объектно ориентированное, обобщённое, функциональное, императивное, аспектно ориентированное, событийно ориентированное, прототипное программирование Появился в: 1995 Автор(ы) …   Википедия

  • JavaScript — Paradigmen: multiparadigmatisch Erscheinungsjahr: 1995 Entwickler: Brendan Eich Aktuelle Version: 1.8  (2008) …   Deutsch Wikipedia

  • Dependency injection — (DI) is a design pattern in object oriented computer programming whose purpose is to improve testability of, and simplify deployment of components in very large software systems. The Dependency Injection pattern involves at least three elements:… …   Wikipedia

  • Sprachelemente von C-Sharp — Dieser Artikel bietet eine Übersicht einiger Sprachelemente von C#. Inhaltsverzeichnis 1 Bedingte Ausführung (if, else, switch) 2 Schleifen (for, do, while, foreach) 3 Die Sprunganweisungen break, c …   Deutsch Wikipedia

  • Preprocesador de C — Saltar a navegación, búsqueda El preprocesador de C (cpp) es el preprocesador para el lenguaje de programación C. Es el primer programa invocado por el compilador y procesa directivas como #include, #define y #if. Estas directivas no son… …   Wikipedia Español

  • Comparison of C Sharp and Visual Basic .NET — The correct title of this article is Comparison of C# and Visual Basic .NET. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • Команда (шаблон проектирования) — У этого термина существуют и другие значения, см. Команда. Шаблон проектирования Команда Command Тип: поведенческий Назначение: для обработки команды в виде объекта …   Википедия

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”