For statically typed languages, type of
the variable is known at compile time. C, C++, Java etc. are examples of
statically typed languages. In static type checking, types are associated with
variables not values.
Test.java
public class Test { public static void main(String args[]) { String message="Hello World"; int age=26; } }
One of the main advantage of statically
typed languages is type check is done at compile time, so you can rectify many type
errors at compile time itself. Program execution may be more efficient than dynamically typed languages, since no need to perform type checks at run time.
For dynamically typed languages type of
the variables is interpreted at run time. In dynamically typed languages, type of the variables associated with values not variables. In some dynamically
typed languages you can specify type also, but it is optional. Python, JavaScript,
Julia etc., are examples of dynamically typed language.
test.py
data="Hello world" print(type(data)) data=123.34 print(type(data)) data=True print(type(data))
Output
<class 'str'> <class 'float'> <class 'bool'>
As you observe above python script,
initially I assigned a string to variable named ‘data’, next float, next Boolean.
It works absolutely fine, but it is not the case with statically typed
languages, you can’t assign incompatible data to a variable (like you can’t
assign string to an integer, string to Boolean etc.,).
You may like
No comments:
Post a Comment