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.
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).
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.
var greeting = "Hello!"; var someRandomInteger = 100; var aDoubleVariable = 2.2;
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.
"Hello there!", the engine will realize that this is a
String and it will assign that as the datatype behind the scenes.
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.
You just need to sit back and enjoy your new-found freedom of not having to think about what types to assign to your variables when you declare them! Just declare those variables and walk away 🙂
And as always, be sure to join our email list below and receive a free gift directly to your inbox. You can read more about what I’m giving away as a free gift below this post!