typeof, alternately also typeOf, and TypeOf, is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.
In languages that support polymorphism and type casting, the typeof operator may have one of two distinct meanings when applied to an object. In some languages, such as Visual Basic, [1] the typeof operator returns the dynamic type of the object. That is, it returns the true, original type of the object, 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# [2] or D [3] and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions), [4] [5] the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form. These languages usually have other constructs for obtaining run-time type information, such as typeid.
As of C23 typeof
is a part of the C standard. The operator typeof_unqual
was also added which is the same as typeof
, except it removes cvr-qualification and atomic qualification (differentiating it from decltype
in C++). [6] [7]
In a non-standard (GNU) extension of the C programming language, typeof
may be used to define a general macro for determining the maximum value of two parameters:
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
In C++, while there is no typeof
operator, there is a decltype
operator, which can be used to represent the type of a variable. Although C23 added typeof
, it does not exist in C++. Unlike C23 typeof
, decltype
does not strip const-volatile-reference and atomic qualification.
structA{doublex;}constA*a;// decltype can be used in type declarationsdecltype(a->x)y;// equivalent to double y;// decltype can be used in trailing return types// This may be necessary for overloaded operators where, for instance,// type T + type U may result in type V distinct from T and Utemplate<typenameT,typenameU>autoadd(Tt,Uu)->decltype(t+u){returnt+u;}
In C#:
// Given an object, returns if it is an integer.// The "is" operator can be also used to determine this.publicstaticboolIsInteger(objecto){returno.GetType()==typeof(int);}
Java does not have a keyword equivalent to typeof
. All objects can use Object
's getClass()
method to return their class, which is always an instance of the Class
class. All types can be explicitly named by appending ".class
", even if they are not considered classes, for example int.class
and String[].class
. There is also the instanceof
operator for type introspection which takes an instance and a class name, and returns true for all subclasses of the given class.
Strings="Hello, world!";Class<?>sClass=s.getClass();Class<String>stringClass=String.class;Class<Integer>intClass=int.class;Objectobj=/* something here */;System.out.printf("The class of %s is %s%n",obj,obj.getClass().getName());
In JavaScript:
functionisNumber(n){return(typeofn==='number');}
In TypeScript: [8]
function(param:typeofexistingObject){...}
letnewObject:typeofexistingObject;
Python have the built-in function type
. [9]
>>> type(123)<class 'int'>
In VB.NET, the C# variant of "typeof" should be translated into the VB.NET's GetType method. The TypeOf keyword in VB.NET is used to compare an object reference variable to a data type.
The following example uses TypeOf...Is expressions to test the type compatibility of two object reference variables with various data types.
DimrefIntegerAsObject=2MsgBox("TypeOf Object[Integer] Is Integer? "&TypeOfrefIntegerIsInteger)MsgBox("TypeOf Object[Integer] Is Double? "&TypeOfrefIntegerIsDouble)DimrefFormAsObject=NewSystem.Windows.Forms.FormMsgBox("TypeOf Object[Form] Is Form? "&TypeOfrefFormIsSystem.Windows.Forms.Form)MsgBox("TypeOf Object[Form] Is Label? "&TypeOfrefFormIsSystem.Windows.Forms.Label)MsgBox("TypeOf Object[Form] Is Control? "&TypeOfrefFormIsSystem.Windows.Forms.Control)MsgBox("TypeOf Object[Form] Is IComponent? "&TypeOfrefFormIsSystem.ComponentModel.IComponent)