paint-brush
How To Understand The Difference Between Statically - Dynamically - Strongly - Weakly Typed Languageby@jarpit96
11,006 reads
11,006 reads

How To Understand The Difference Between Statically - Dynamically - Strongly - Weakly Typed Language

by Arpit JainOctober 14th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The Difference Between Statically - Dynamically - Strongly - Weakly Typed Languages. Type checking is the process of verifying and enforcing the constraints of types. Strongly typed languages have a stronger type-check and enforce the type assigned (at compile or run time) Weakly typed language allows expressions between various different types. Java Specification calls Java a strongly typed language, but The First C Programming Language uses no such term. The First Programming Language C. Edition uses Java to define strongly typed languages.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How To Understand The Difference Between Statically - Dynamically - Strongly - Weakly Typed Language
Arpit Jain HackerNoon profile picture

First of all, what are all these words - Statically - Dynamically - Strongly - Weakly Typed Languages?
This is how you can classify programming languages:

  • Statically typed vs Dynamically typed programming languages 
  • Strongly typed vs Weakly typed programming languages.

We will understand what these terms mean so you don't have to look them up in your next flame war.

So let’s dive right in :


Type Checking

It’s the process of verifying and enforcing the constraints of types. Usually performed by the compiler or interpreter at compile or run time. For instance you can’t divide a string by a floating point number (if you can, please don't).

In simpler terms, type checking is just looking at variables and their types and then saying does this expression make sense.

So, now we know what type checking is, understanding these terms is really simple. 

In statically typed languages type checking happens at compile time. Whereas, in dynamically typed languages, type checking happens at run-time.

What Does That Mean For You? 

Type declaration
Static: All variable types have to be explicitly stated as this information is required at compile time.
For example in Java   

float f = 0.5;

Dynamic: Explicit declaration is not required as type is assigned to a variable at runtime.
For example in Python

f = 0.5

Performance
Static: Do more processing at compile time but give better run-time performance.

Dynamic: More efficient compiler/interpreters, but type-checking at run-time affects performance.

Flexibility and Errors 
Static: Is less prone to runtime errors but provide less flexibility to programmer.

Dynamic: Provides more flexibility but more prone to runtime errors.

Remember It
A quick hack to remember what are statically typed and dynamically typed language, is to call them by their full names

==> Statically type-checked languages. 

==> Dynamically type-checked languages.

The language cloud. Courtesy: Mayank Bhatnagar.

But What are Strongly Typed and Weakly Typed Languages?

It’s a spectrum* (disclaimer at the bottom). So, we will just go ahead and learn the terms the way they are often used.

In Strongly typed languages once a type is assigned to a variable say at run time or compile time, it retains that type and can’t be intermingled in expressions with other types easily. 

For example in Python 

data = “string1”         //Type assigned as str at runtime
data = 5   //Type assigned as int at runtime
data = data + “string2” //Type-error str and int can’t be concatenated

Whereas, in Weakly typed languages, once a type is assigned to a variable say at run-time or compile-time, it can be intermingled in expressions with other types easily.

For example in Javascript 

$data = “string1”        //Type assigned as str at runtime
$data = 5 //Type assigned as int at runtime
$data = $data+“string2” //str and int get concatenated

Conclusion

Type checking ensures that correct variable types are part of an expression. In Statically type-checked languages, type checking happens at compile time, whereas in dynamically type-checked languages, type checking happens at run-time.

Strongly typed languages have a stronger type-check and enforce the type assigned (at compile or run time), whereas, weakly typed languages have a weak type-check and allows expressions between various different types.

(Fun?) Disclaimer

If you go into the nitty gritty and try to find THE right answer, you will come back discombobulated (exactly!).

Turns out, there is no official demarcation of these terms agreed throughout the industry. It’s a spectrum.

But the terms still get thrown around a lot. So, I went digging. Java Language Specification calls Java a strongly typed language.

But The C Programming Language First Edition uses no such term.

References

    Find me on Twitter, Facebook, Linkedin, Quora, Github, Medium, Gmail @jarpit96