filmov
tv
EP21 – Dynamic Typing vs Static Typing

Показать описание
Check out more great lessons via this link:
Generally speaking, modern day programming languages fall into one of two categories:
Dynamic Typing
Static Typing
Before we can dive into what dynamic or static typing means, we’ll first need to understand what it means by “typing” in general.
Note: This concept actually reaches beyond just JavaScript and Java, but for the scope of this conversation we will be using JavaScript as the example for dynamic typing and Java as the example for static typing.
What is Typing in Programming?
The first place I want to start is to talk a little bit about the concept of typing.
When we say “typing” we’re referring to the concept of applying a “type” to a variable.
I’m going to assume you’re familiar with Java already, but if you’re not, what better place to learn about Java than right here!
So, in Java, we always assign a type to our variables. For example:
String greeting = "Hello!";
Integer someRandomInteger = 100;
Double aDoubleVariable = 2.2;
In all three of these variable declarations above, we have assigned a variable type to each variable. The reason we have done this is because Java is statically typed and therefore we are forced to assign an exact type to the variables before we can use them.
Having a type is important in programming because it allows the language to decide what to do with your code in certain situations.
For example, if you have two Integers and you tell it to add the two together, because they are Integers, the language knows to use addition… however, if you had two Strings and added them together, because they are Strings, the language knows to concatenate them (not add them).
Make sense?
Types are important.
Having said that…
Dynamic vs Static Typing
The difference between dynamic and static typing is that with dynamic typing, the programmer is not required to specify any types for the variables they create.
How neat is that?
This means that with languages that use dynamic typing, you don’t actually have to decide if the variable you’re creating should be a Double or a Float or a String or an Integer etc.
All you need to do is just tell the language that you want to create a variable and you go about your business assigning whatever values you like to it.
So let’s recreate the declaration of those three Java variables using the JavaScript syntax:
var greeting = "Hello!";
var someRandomInteger = 100;
var aDoubleVariable = 2.2;
When we execute this code in JavaScript, the JavaScript interpreter (a.k.a the JavaScript engine) will look at the values that are being assigned to the variables you’ve declared and decide for itself what “type” to assign the variables in the back-end.
Having said that, it’s very important to understand that just because you didn’t have to assign any types to your variables, doesn’t mean that JavaScript doesn’t have types… because JavaScript definitely does have types.
You’ll be introduced to the typeof operator in a couple of weeks. It’s used to determine what type is assigned to your variables.
How Dynamic Typing Works in JavaScript
The concept behind how dynamic typing works in JavaScript is actually pretty straightforward.
The JavaScript engine will choose a type that it feels best describes the data that’s contained inside of your variable. So when you create a variable and assign it the value of "Hello there!", the engine will realize that this is a String and it will assign that as the datatype behind the scenes.
This means that if the JavaScript engine has chosen the String datatype in the back-end, and you were to add another string to this variable, it would know to perform string concatenation and not addition.
So all the same rules of variables that you’re already familiar with, still apply. There’s just a algorithm that works on your behalf to assign a type to your variables.
We’ll go into more details about what implications dynamic typing has to your coding in the next article when we dive further into JavaScript va
Generally speaking, modern day programming languages fall into one of two categories:
Dynamic Typing
Static Typing
Before we can dive into what dynamic or static typing means, we’ll first need to understand what it means by “typing” in general.
Note: This concept actually reaches beyond just JavaScript and Java, but for the scope of this conversation we will be using JavaScript as the example for dynamic typing and Java as the example for static typing.
What is Typing in Programming?
The first place I want to start is to talk a little bit about the concept of typing.
When we say “typing” we’re referring to the concept of applying a “type” to a variable.
I’m going to assume you’re familiar with Java already, but if you’re not, what better place to learn about Java than right here!
So, in Java, we always assign a type to our variables. For example:
String greeting = "Hello!";
Integer someRandomInteger = 100;
Double aDoubleVariable = 2.2;
In all three of these variable declarations above, we have assigned a variable type to each variable. The reason we have done this is because Java is statically typed and therefore we are forced to assign an exact type to the variables before we can use them.
Having a type is important in programming because it allows the language to decide what to do with your code in certain situations.
For example, if you have two Integers and you tell it to add the two together, because they are Integers, the language knows to use addition… however, if you had two Strings and added them together, because they are Strings, the language knows to concatenate them (not add them).
Make sense?
Types are important.
Having said that…
Dynamic vs Static Typing
The difference between dynamic and static typing is that with dynamic typing, the programmer is not required to specify any types for the variables they create.
How neat is that?
This means that with languages that use dynamic typing, you don’t actually have to decide if the variable you’re creating should be a Double or a Float or a String or an Integer etc.
All you need to do is just tell the language that you want to create a variable and you go about your business assigning whatever values you like to it.
So let’s recreate the declaration of those three Java variables using the JavaScript syntax:
var greeting = "Hello!";
var someRandomInteger = 100;
var aDoubleVariable = 2.2;
When we execute this code in JavaScript, the JavaScript interpreter (a.k.a the JavaScript engine) will look at the values that are being assigned to the variables you’ve declared and decide for itself what “type” to assign the variables in the back-end.
Having said that, it’s very important to understand that just because you didn’t have to assign any types to your variables, doesn’t mean that JavaScript doesn’t have types… because JavaScript definitely does have types.
You’ll be introduced to the typeof operator in a couple of weeks. It’s used to determine what type is assigned to your variables.
How Dynamic Typing Works in JavaScript
The concept behind how dynamic typing works in JavaScript is actually pretty straightforward.
The JavaScript engine will choose a type that it feels best describes the data that’s contained inside of your variable. So when you create a variable and assign it the value of "Hello there!", the engine will realize that this is a String and it will assign that as the datatype behind the scenes.
This means that if the JavaScript engine has chosen the String datatype in the back-end, and you were to add another string to this variable, it would know to perform string concatenation and not addition.
So all the same rules of variables that you’re already familiar with, still apply. There’s just a algorithm that works on your behalf to assign a type to your variables.
We’ll go into more details about what implications dynamic typing has to your coding in the next article when we dive further into JavaScript va
Комментарии