You Need To Know Compiled/Interpreted, Static/Dynamic, and Strong/Weak Typing
Tags: programming, computerscience, python, javascript
Published: 2021-05-03 05:05:43

<!-- wp:heading {"level":4} -->
<h4>In this article, I’ll try to demystify the terms static/dynamic typing, strong/weak typing, and define some terms like run-time, compilation, etc</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you have taken a few programming courses, you might have come across the following statements and somewhat understood</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>“Python is strongly, dynamically typed”</li><li>“JavaScript is weakly, dynamically typed”</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>After reading this article, you will be able to clearly understand the above statements. We will be looking at code snippets in Python, JavaScript and C++. If you do not know the syntax or any of the languages, it’s ok, you will still be able to understand the underlying concepts.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Table of&nbsp;Contents</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Some Definitions and Points to Remember</li><li>Compiled vs Interpreted Languages</li><li>Static Typing vs Dynamic Typing</li><li>Weak Typing vs Strong Typing</li><li>Classifying Programming Languages</li><li>Conclusion</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Some Definitions and Points to&nbsp;Remember</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The word “typing” in static typing/dynamic typing refers to the <strong>DATATYPE</strong>. It doesn’t refer to the act of pressing keys. I was confused about this and I am sure there might be others in the same boat</li><li>Programming languages are classified as Static/Dynamic types and Strong/Weak type based on two different properties, i.e Static/Dynamic typing is not related to Strong/Weak typing</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Definitions</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><strong>Source Code</strong>- This is the code written in Python, JavaScript, C++, etc. Also known as <strong>human-readable code</strong></li><li><strong>Machine Code</strong>- This is the code that is written in Machine Language (usually binary) and can be understood by a Machine.&nbsp;</li><li><strong>Compiler- </strong>A computer program that can convert the source code into Machine Code</li><li><strong>Compilation</strong>- Transforming code from Python, JavaScript, C++ to low-level machine code. Basically converting human-readable code to machine code</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Assuming compilation is successful, i.e the source code is syntax-error free, we move on to Runtime.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Runtime</strong>- After compilation, the program runs, and the code is executed. This time period is known as runtime.</li><li><strong>Execution</strong>- Actually running/executing the code. This happens after the source code is converted to machine code and during Runtime</li><li><strong>Syntax Error- </strong>Error in the syntax (grammar) of the language. Eg: missing a semicolon in C++, an improper indentation in Python, etc</li><li><strong>Run-time Errors</strong>- A <strong>runtime error </strong>is an error that occurs during program execution eg: division by 0, trying to access an undefined variable, using an operator on incompatible datatypes</li><li><strong>TypeCasting</strong>- Converting a value from one data type to another, eg: a char can be converted to an int based on its ASCII value. Similarly a number 10 can be converted to a string “10”. Type casting can either be <strong>implicit</strong> (it is done by the language automatically) or <strong>explicit</strong> (you manually change the type)</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Compiled vs Interpreted Languages</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A language is classified as compiled/interpreted based on how the source code is converted to machine code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I’ll borrow this analogy from <a href="https://www.freecodecamp.org/news/compiled-versus-interpreted-languages/#:~:text=Interpreted%20vs%20Compiled%20Programming%20Languages:%20What%27s%20the%20Difference?,-Every%20program%20is&amp;text=In%20a%20compiled%20language,%20the,reads%20and%20executes%20the%20code" rel="noreferrer noopener" target="_blank">FreeCodeCamp</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Basically consider you have a recipe written in German and need help from your friend to translate the recipe to English and follow it. There are a couple of ways you could go about this</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>You could translate each step one by one, i.e first your friend translates Step 1 and you follow it. Then you move on to the second step, your friend translates it and you follow it. And so on…..</li><li>You translate the entire recipe at once and then follow it, i.e your friend translates the entire recipe to English. Now, you follow it step by step.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The first approach is similar to “Interpreted” while the second approach is similar to “Compiled”</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><strong>Interpreted Languages</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The program’s source code is converted to machine code and executed line by line. If you define a function but never invoke it, the function is not executed and as a result, any run-time error in the function will not result in an error.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s consider an example in Python</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*0iYWIzMZR_n15A0ZC7CqOQ.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Although our function has a division by 0 (Runtime Error), the program runs/executes successfully. Since we never call the function <strong>func, </strong>the function is never executed and hence the runtime error is never raised</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s consider a similar example in JavaScript</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*OvRACD3v_phbzwB07QKAcg.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Like Python, JavaScript doesn’t raise any errors either. The program is executed successfully since we never invoke the function.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Note: If you did invoke the function in any of the above code-snippets, it would cause an&nbsp;error</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Let’s consider another example in Python</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*9uXTcTaM5p8d3BllHi4SHA.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This time, we have a division by 0 outside a function. This causes Python to raise an error. However, noticed that the print statements are still executed and their output is shown in the terminal. This is because each line is executed one by one. Therefore, when we are at the third print statement, Python hasn’t even executed the division by 0 and as a result, no error is raised till that point</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><strong>Compiled Languages</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The entire source code is converted to machine code. After the conversion, the program is executed. Therefore any runtime error in a function is raised, irrespective of whether the function is invoked or not.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s consider a C++ code snippet</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*4Ep93BYS9UaT7OiBlTA_bw.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In the function func, we assign a value 10 to variable <strong>var</strong> without defining the variable (Runtime Error). Although we don’t invoke the function, the error is still raised. “Hello World” is not displayed in the terminal.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s consider another snippet</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*8_lmx52SVsUorDfHYKVKWA.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Unlike Python or JavaScript, the “cout” statement is not executed. First, the entire source code is converted to machine code and the run-time error is raised. As a result, “Hello World” is not displayed in the terminal. The error is raised since we have not defined the variable <strong>var</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Static Typing and Dynamic&nbsp;Typing</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Static Typing</h4>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>In statically typed languages, a variable has a FIXED data type during its lifecycle.&nbsp;</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Type Checking is done during compile-time</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Consider the C++ code snippet below</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*u-fiL0_k441Pit5SrowjGw.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Initially, we declare variable <strong>var1 </strong>as an int and assign it an integer value. But later, we re-assign it a string. C++ is a statically typed language and doesn’t support this behavior.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>Eg: C, C++, Java, Rust, Go, Scala</strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":4} -->
<h4>Dynamic Typing</h4>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>In dynamically typed languages, the data type of a variable can be changed during it’s life&nbsp;cycle</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Type Checking is done during run-time</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s try to do the same thing we did above but in a dynamically typed language like Python.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*UIJSWf2kFfgNQWO62OJZTA.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Initially variable <strong>var1 is an integer variable. However</strong>, later we assign a string value and the data type of the variable changes. A dynamically typed language like Python allows us to change the data type of a variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s do something similar in JavaScript, another dynamically typed language.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*KpIfaojWx3SlFGKJ1aOlPw.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Note: if you use the <strong>const </strong>keyword, you can not change the data type of the the variable. Constant values can not be re-assigned, therefore in a way their data type is static. However, Java Script is a dynamically typed language since the data type of a VARIABLE can change during it’s lifecycle</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>Eg: Perl, Ruby, Python, PHP, JavaScript</strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":3} -->
<h3>Weak Typing and Strong&nbsp;Typing</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Weak Typing</h4>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Weakly typed languages support operations between different data types. These languages support implicit type casting and one of the data types is converted to the&nbsp;other.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Let’s consider a code snippet in C++</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*H9JqY_wqY4DGmwWihtP88g.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The ascii value of <strong>a</strong> is <strong>97</strong>. When we try to do <strong>10+a</strong>, C++ implicitly typecasts a to it’s <strong>ascii value(97)</strong>. Therefore the output is 107.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s do something similar in JavaScript.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*UxxiqlYlt6_AK0Z1GtmMkg.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>JavaScript convert the number <strong>10</strong> to a string <strong>“10” </strong>and concatenaded it to<strong> a</strong></p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>Eg: C/C++, JavaScript, PHP, Perl</strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":4} -->
<h4>Strong Typing</h4>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Strongly typed languages DO NOT support operations between different datatypes. They might/might not support typecasting. However, they do not implicitly typecast&nbsp;data.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Let’s consider a code snippet in Python. We’ll try to do the same thing we did above.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*sze1ohLGlRpcLX3fWo7gyg.png" alt=""/><figcaption>Code Snippet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>When we try to add an integer and a string, we get a Type Error. This makes Python Strongly type. However, notice that it is possible to explicitly typecast the integer to a string and perform the operation.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>Eg: Python, C#, Java, Scala, Ruby</strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":3} -->
<h3>Classifying Programming Languages</h3>
<!-- /wp:heading -->

<!-- wp:image {"linkDestination":"custom"} -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*LZvZgrN2sEebmd8ZEMP6XQ.png" alt=""/><figcaption><a href="https://medium.com/android-news/magic-lies-here-statically-typed-vs-dynamically-typed-languages-d151c7f95e2b" rel="noreferrer noopener" target="_blank">https://medium.com/android-news/magic-lies-here-statically-typed-vs-dynamically-typed-languages-d151c7f95e2b</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Conclusion</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I hope I was able to explain the terms and how a language is determined to be compiled/interpreted, static/dynamic, weak/strong. Knowing these properties about a language will let you know what to expect when starting out with that language. Please let me know if you find any mistakes or know of any good analogies to explain the difference&nbsp;:)</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Connect with me on <a href="https://www.linkedin.com/in/rahulbanerjee2699/" rel="noreferrer noopener" target="_blank">LinkedIn</a>, <a href="https://twitter.com/rahulbanerjee99" rel="noreferrer noopener" target="_blank">Twitter</a></p></blockquote>
<!-- /wp:quote -->