Strictly typed programming languages are languages that require you to specify the data type of a variable when declaring it. This means that you can only assign values of the specified data type to the variable, and attempting to assign a value of a different data type will result in an error.
Java is a prime example of a strictly typed language. In Java, you must specify the data type of a variable when declaring it, using one of the predefined data types or a user-defined type.
For example:
int num = 123;
String name = "Jack";
In this example, we declare a variable num
and assign it the value 42
, which is an integer. We then declare a variable name
and assign it the value "Jack"
, which is a string. We must specify the data types of these variables when declaring them, and we can only assign values of the corresponding data types to them.
num = "Jack"; // This will cause a compile-time error
name = 123; // This will also cause a compile-time error
In this example, we attempt to reassign the variables num
and str
to values of different data types. However, this is not possible in Java because it is a strictly typed language, and we will receive a compile-time error if we try to do this.
Strictly typed languages can be more reliable and easier to debug than loosely typed languages, because they enforce stricter rules about data types and can catch type errors at compile-time.
They can also be more cumbersome to work with, as you must specify the data type of every variable and may need to perform type conversions when working with different data types.
If you’re not sure if strictly typed programming languages are a good choice for you, start by learning about loosely typed programming languages.