Statically typed and dynamically typed languages are two different approaches to type checking in programming languages.
In a statically typed language, the type of a variable is determined at compile time and cannot be changed later. This means that the programmer must declare the type of each variable and the compiler checks that each variable is only used in ways that are consistent with its type. Common examples of statically typed languages include Java, C++, and Pascal.
Here is an example of a statically typed language:
int x = 5;
float y = 10.0f;
String name = "John";
In this example, the variables ‘x‘ and ‘y‘ have a specific type (‘int‘ and ‘float‘, respectively), and their types cannot be changed later. The variable ‘name‘ has a type of ‘String‘, which is a class that represents a sequence of characters.
In contrast, in a dynamically typed language, the type of a variable is determined at runtime and can be changed later. This means that the programmer does not need to declare the type of each variable, as it can be inferred by the interpreter or compiler at runtime. Common examples of dynamically typed languages include Python, JavaScript, and Ruby.
Here is an example of a dynamically typed language:
x = 5
y = 10.0
name = "John"
In this example, the variables ‘x‘, ‘y‘, and ‘name‘ do not have specific types declared. Their types can be inferred at runtime, based on the values assigned to them. For example, ‘x‘ has an integer type, ‘y‘ has a floating point type, and ‘name‘ has a string type.
One advantage of dynamically typed languages is that they often require less code to accomplish the same task as in a statically typed language. Dynamic typing also allows for more flexibility in coding, as variables can be assigned different types at runtime. However, static typing can catch errors early in the development process, which can lead to fewer bugs and easier maintenance of the codebase.