Generally speaking, modern day programming languages fall one of these 2 categories: dynamically typed vs statically typed.
So what is the difference between a dynamically typed and a statically typed programming language?
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 a Statically Typed Language?
Let's start by talking about the concept of “typing”.
When we say “typing” we're referring to the concept of applying a “type” to a variable.
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 dynamically typed and statically typed programming language is that with dynamic typing, the programmer is not required to specify any types for the variables they create.
How neat is that?
But I'll need to dig in just a bit further to refine my definition of a dynamically typed language. You see, if you don't explicitly set a type, but the programming language can assign a type at compile time, then the language is still considered to be statically typed even though the programmer isn't required to explicitly set a type.
A programming language is said to be dynamically typed if the programmer doesn't have to assign a type explicitly, and a type isn't assigned to the variable until run-time.
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!