Static Type Binding Vs Dynamic Type Binding

bindingdynamictypes

I understand what is meant by type binding . Please correct me if I am wrong –

Type binding is the process of 'associating' a declared variable to a particular type
(Done by the compiler) .

Type binding can be classified as –
Static type binding
Dynamic type binding

Static type binding can be achieved by two types of declarations –

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.2.gif

Its clear till here .
But now , what is Dynamic type binding ? (not the definition) I know that it means that a variable is associated with a 'type' during run-time ,

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.3.gif

Any more information on it , like –

  1. Why Dynamic type binding ?
  2. In which programming languages is it available as a feature ?
  3. What are its advantages and disadvantages over static type binding ?

Specified through an assignment statement

Can you give more information on it . Should it be specified only with an assignment statement ?

Best Solution

I will try to answer your questions:

First the easiest In which programming languages is it available as a feature ?

Php, Python, Ruby, Perl, JavaScript ...

"What are Dynamic Type Binding?"

Programming languages that use this approach to bind a variable type,like PHP, Python, Ruby and so on, are in general interpreted languages. So, they do not have a compiler. On those languages you do not specify the variable type and do not have implicity rules for this, so it's "impossible" to discover a variable type before the running type.

Why use dynamic type binding?

To answer this question, I have to talk about some advantages of this approach. To choose use Dynamic Type Binding is a decision about your language design. So, depends of what you want for your language and of which situation it will be used.

Advantages

  • It is more easy to write generic code.

Ex: Think about build a generic data structure, for instance Stack. You want to use it to keep all kind of variables using different stacks, for instance, a stack for int , another for string and so on. To do this on ** programming languages with static type binding** it is more hard.

In C for instance you have two options:

  • Write one stack for each type you want to use keep on stack
  • Build a stack that keeps (void *), in other words, a point for anything

If you try to do this in Python, you only write your code at once. The language does the hard work for you.

You can argue about Java and other languages, but you have to keep in mind that, dynamic type binding is concept older than Oriented Object Programming, so on that time, there was not Polymorphism.

Disadvantages

  • High Cost to check type and interpretation

Execute a program using a binary file generated by compilation is much more faster than the same program running above a interpreter. The second thing is, when you are using static type binding, "basically, the binding process is done just once, before execution. In other hand, in languages with Dynamic type binding, the binding process is done o each assignment operation of each variable.

Other minimal disadvantages is about some erros that a compiler could find, but using dynamic type binding the interpreter can not.

For example: As you can change the type of your variables as many time as you need during execution time, is really possible make a confusion, like change a variable of int to string, and some point after, try to call a function that receives a int and you call this function using the variable that now is string.

This is a minimal problem, because an experient developer will never do this, but is still possible and in languages with static type binding is not.

You have to keep in mind that we are talking about concepts, and implementations of those concepts are evolving together with computer science, so probably some Advantages/Disadvantages can be minimized on nowadays.