What's your type? A short guide to data typing. 
Tags: beginners, computerscience, programming
Published: 2020-11-16 18:56:43

---
title: What's your type? A short guide to data typing. 
published: true
description: Determining differences between static typing, dynamic typing, weak or strong - this is your guide
tags: beginners, computerscience, programming
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/n6i0b49m1wfbgt62if6i.jpg
---

##TLDR
Typing - way in which computers determines different data formats.

**Static Typing** - Type defined at compile time
**Dynamic Typing** - Type defined at run time
**Weakly Typed** - Variables can change type at run time
**Strongly Typed** - Variables cannot change type at run time

##Read on....

When I first stated learning development, I used to think of "typing" as "typing on a keyboard" rather than "data typing" 
which is why everything was a little bit more confusing at the start. You live and learn tho. 

#What is typing?

Data typing is a way of describing the type of data we are using. For example, we know that a boolean can only be True or False, or 0 and 1. 
This process allows the compilers to determine the required memory to hold data and ensure that the program can be run with no type errors. 

#Types, types, types

###Static and Dynamic typing

If we define the type of data when we declare a new variable such as : 

```java
//Java
boolean isValid = true;
int number; 
```
then this is known as **Static Typing** where type is explicitly typed before the compiler. Languages such Java or C# as do this.
Attempts to re-assign the variable type will result in an error:

```java
//Java
boolean isValid = "naw, mate"; //compilation error
```

The type checking in this case, is done at *Compile Time* ( although many IDE's will let you know ahead of time, they are good that way).

If we do not need to specify the type which we want our data to be, it is determined at run time. The language then has **Dynamic Typing** and examples include JavaScript or Python. 

```javascript
//javascript
isValid = true;
```

###Strongly and Weakly typed languages

Some languages allow you to re-assign the type a variable can hold at runtime - for example Javascript. This a quality of **weakly typed** languages.

```javascript
//javascript
isValid = true;
isValid = "I am";
```
Where as in others, this is not the case:

```java
boolean hello = true; 
hello = "hello";
//error: incompatible types: String cannot be converted to boolean
```

### Some flexible languages

Sometimes, languages are super flexible, like Go. Whilst being usually considered a strongly typed static language, it allows the compiler to determine the type, but once its set, you cannot change it: 

```Go
isValid := true
isValid = 1

//cannot use 1 (type untyped int) as type bool in assignment
```

###Is static faster than dynamic?

In theory, yes, because the compiler doesn't need to work out what data type it needs. 

In practice, this is not the only way to measure performance. In [Java vs Go comparison](https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/go.html), both languages are statically and strongly typed and yet the performance varies due to a multitude of other factors. 
In addition, different languages have different use cases, and have been developed with different purposes in mind
(whether they have kept to it 10yrs down the line, thats a different issue) and so sometimes its like comparing [Apples and Oranges](https://benchmarksgame-team.pages.debian.net/benchmarksgame/sometimes-people-just-make-up-stuff.html). 

 
#Should I change the type during the running of my code?

As always, it depends. 

But in majority of the cases, I would argue that this is a code smell and show be avoided where possible. Typescript was created in the big part, to help JS devs eliminate type bugs!

In certain cases it does make sense.


Happy coding!