CSC/ECE 517 Fall 2009/wiki1b 2 ps: Difference between revisions
Line 34: | Line 34: | ||
== Dynamically typed languages == | == Dynamically typed languages == | ||
In dynamically typed languages, the types are associated with the values. The run-time checker identifies the type errors at the run time [1]. Consider the code of the program below, since the type information is not known at the compile time, the addition of the character and an integer will result in run-time error. | |||
a = [‘x’, 1, 2] | |||
b = a[0] + a[1] //Type Error | |||
=== Advantages and Disadvantages=== | === Advantages and Disadvantages=== | ||
== Comparison of statically typed and dynamically typed languages == | == Comparison of statically typed and dynamically typed languages == | ||
== References == | == References == |
Revision as of 14:05, 21 September 2009
Advantages of statically typed vs. dynamically typed languages
Type checking can be performed in programming languages either during run-time or at compile time[1]. Based on such a behavior of the type system, the programming languages are classified as statically typed or dynamically typed. The statically typed languages like C, C++, C#, Java, FORTRAN etc., eliminate the type checks to be performed every time the program is executed [1]. The condition checked once will hold good for all the future executions of the program as well. The dynamically typed languages like PHP, Prolog, Python, Ruby, Smalltalk etc., perform the run-time checks uses the information from the compile time as well and hence could be more comprehensive[1].
Typed and Untyped languages
The “type” provides the information about the chunk of memory which holds the data and thus categorizes it such that the valid operations that can be performed on that data are defined. In object-oriented programming, the types include classes (user-defined), strings and integers (primitive types), lists/arrays/vectors/hash (containers) [2]. A variable in a program can accept a certain range of values during its execution. The upper bound value for that range marks the type for that particular variable. For example, if there is a variable num of type int in the program then num can accept only integer values during every execution of the program. Such languages where all the variables are associated with the data type are called typed languages. Certain languages do not specify separate data types for each of the variables but contains a single universal type that holds all the values. They do not define the range for the variables and are called untyped languages (assembly languages) [4].
Type Checking
Type checking is done only for the typed languages. All the variables and expressions in the program are associated with the data type. Errors might occur due the explicit or implicit mismatch of the type information. Type checking identifies the errors that may or may not produce wrong computations in the program [3]. In order to eliminate these errors the programming languages have the type systems. Type system is programming language specific and defines the rules of how the typed programs should behave. The programs which do not follow these rules produce error [1]. The type system performs the static check on the statically typed languages and dynamic check on the dynamically typed languages.
Statically typed languages
In statically typed languages, types are associated with the variables or the expressions and not the values. The type checking is performed at the compile time and thus many types of errors can be identified in the early phases of the development cycle [1]. Consider the code of the program below. It shows the illegal statements in the code the static-type checker will identify.
int num; num = “Assign a string”; //Type error ArrayList aList = new ArrayList(); //Collection from Java int array[] = aList; //Type Error
Advantages and Disadvantages
If the static type checks are performed once for a program, there is no necessity to perform the check at every execution of the program. Thus the execution can be efficient by eliminating the run-time checks. Certain programs may exhibit a desired behavior at run-time but since the static type checkers do not have the type information that might be available only during run-time, they might consider identify errors is such programs even though they might be resolved at run-time[1]. For example, consider the code in a program –
int x; if (expression is true) then <perform computation> else x = “Error” // int cannot take the string “Error” – type error
Dynamically typed languages
In dynamically typed languages, the types are associated with the values. The run-time checker identifies the type errors at the run time [1]. Consider the code of the program below, since the type information is not known at the compile time, the addition of the character and an integer will result in run-time error.
a = [‘x’, 1, 2]
b = a[0] + a[1] //Type Error
Advantages and Disadvantages
Comparison of statically typed and dynamically typed languages
References
[1] http://en.wikipedia.org/wiki/Type_system#Type_checking
[2] http://www.artima.com/weblogs/viewpost.jsp?thread=7590
[3] http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1-show-types-and-check-1.html
[4] http://www.eecs.umich.edu/~bchandra/courses/papers/Cardelli_Types.pdf