Booklet

author

Hack University

Development

Strong vs Weak Typing

Strong vs Weak Typing

To start off...

Let's not confuse Strong / Weak Typing with Static / Dynamic Typing.

They're similar in a way but fundamentally different.

Strong / Weak Typing is about how strictly types are distinguished. (For example: whether the language tries to do an implicit conversion from strings to numbers.)

Static / Dynamic Typing is about when type information is acquired (either at compile time or at runtime).

Now that we cleared that up, let's go deeper into Strong vs Weak Typing

Strongly Typed

Most developers agree that the essence of a strongly typed language is the fact that it won't convert a variable or value's type to suit the current situation automatically. This means that "1234" will always be treated as a string, and won't be usable as a number without manual conversion or intervention.

Let's look at this Python example:

x = y;

y = "2";

z = x=y; #This will fail

This addition operation to get the variable z will fail because the two variables (x and y) which we tried to add together were different types. Python will throw an error that states that adding an int and a string isn't supported.

Examples of strongly-typed languages:

1. Python

2. Perl

3. Ruby

4. Smalltalk

5. Self

6. Lisp

Weakly Typed

The opposite of strongly typed. The interpreter or compiler will attempt to make the best of what's given by using variables in ways that might seem confusing at first, but make sense once we understand what they're doing. Meaning, in some cases for example, an integer might be treated as if it were a string, in order to suit the context of the situation.

Let's look at this JavaScript example:

const x- 1;

const y = "2";

const z = x + y; // z will equal 12

This returned 12, instead of 3. This is because rather than performing a math operation as we requested, it treated both values as strings, and concatenated them together, resulting in the string "12"

Examples of weakly-typed

languages:

1. JavaScript

2. Perl

3. Rexx

4. PHP

5. VB

6. C++

Hopefully these last two posts helped you understand the differences between Statically and Dynamically Typed as well as Strongly and Weakly Typed languages.

Please let us know in the comments if you're still confused and we'd be happy to explain more in depth!

In case you missed it...

Check out our previous post about Static vs Dynamic Typing

Join our tech and design community! @hackuniversity

More booklets by Hack University

More Development booklets

Your recently viewed booklets

  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
See all booklets