Statically Vs Dynamically Typed Languages

Statically Vs Dynamically Typed Languages

·

2 min read

Statically Typed Languages

Statically typed language needs to define variable data type before compiling the program Because the compiler doesn’t understand the type of variable value whether it’s string or integer.

Example:

public class Main
{
    public static void main(String[] args) {
        // datatype variableName = value
      String variableName = "Hello World!";
        System.out.println(variableName);
        // Output: Hello World!
    }
}

As there is a String data type assigned this is why it’s not showing any error but when there will be no data type the compiler will return an error.

Example:

public class Main
{
    public static void main(String[] args) {
        // no datatype assigned
        variableName = "Hello World!";
        System.out.println(variableName);
    }
}

Output:

Main.java:12: error: cannot find symbol
        variableName = "Hello Worlds";
        ^
  symbol:   variable variableName
  location: class Main
Main.java:13: error: cannot find symbol
        System.out.println(variableName);
                           ^
  symbol:   variable variableName
  location: class Main
2 errors

It will return can’t find symbol error due to not defined data type, There are a few languages that are statically typed languages such as C, C++, C#, and Java.

Dynamically Typed Languages

Dynamically typed languages don’t need to define variable data type before compiling the program, Compiler or Interpreter automatically identifies the value’s data type and returns the output.

Example:

#!/bin/python

variable_name = "Hello World!"
variable_name_2 = 10

print(f"{variable_name} : {type(variable_name)}\n\n{variable_name_2} : {type(variable_name_2)}")

# Output:
"""
    Hello World! : <class 'str'>

    10 : <class 'int'>
"""