What is a Typed language ?
Last Updated :
24 Mar, 2022
Typed Language: Typed languages are the languages in which we define the type of data type and it will be known by machine at the compile-time or at runtime.
Typed languages can be classified into two categories:
- Statically typed languages
- Dynamically typed languages
Statically typed languages: Statically typed languages are the languages like C, C++, Java, etc, In this type of language the data type of a variable is known at the compile time which means the programmer has to specify the data type of a variable at the time of its declaration. We have to pre-define the return type of function as well as the type of variable it is taking or accepting for further evaluations.
Syntax:
data_type variable_name;
Example: The below example illustrates the C++ code to show it is statically typed language:
C++
#include <iostream>
#include <string>
using namespace std;
int number( int n){
return n;
}
int main() {
string str = "GeeksforGeeks" ;
int num = 109;
float flo = 12.99;
cout << "I'm a string with value: " << str;
cout << "I'm a number with value: " << number(num);
cout << "I'm a floating point number with value: " << flo;
return 0;
}
|
Output:
I'm a string with value: GeeksforGeeks
I'm a number with value: 109
I'm a floating point number with value: 12.99
Example 2:
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str= "GeeksforGeeks" ;
int num = 109;
float flo = 12.99;
int num2 = "Welcome to GeekdforGeeks" ;
cout << "I'm a string with value: " << str;
cout << "I'm a number with value: " << num;
cout << "I'm a floating point number with value: " << flo;
cout << "I'm a number with value: " << num2;
return 0;
}
|
Output: It will show an error because we can not directly assign the value to a variable other than its defined data type:
prog.cpp: In function ‘int main()’:
prog.cpp:11:13: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
int num2="Welcome to GeekdforGeeks";
^
Dynamically typed language: These are the languages that do not require any pre-defined data type for any variable as it is interpreted at runtime by the machine itself. In these languages, interpreters assign the data type to a variable at runtime depending on its value. We don’t even need to specify the type of variable that a function is returning or accepting in these languages. JavaScript, Python, Ruby, Perl, etc are examples of dynamically typed languages.
Example: This example demonstrates JavaScript as a dynamically typed language:
HTML
< script >
var str = "GeeksforGeeks";
var num = 5;
var flo = 12.99;
var num2 = "Welcome to GFG";
function number(n) {
return n;
}
console.log("I'm a string with value: " + str);
console.log("I'm a number with value: " + number(num));
console.log("I'm a floating point number with value: " + flo);
console.log("I'm a string with value: " + num2);
</ script >
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...