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 code to show it is statically typed language:
#include <iostream>
#include <string>
using namespace std;
int number(int n){
return n;
}
int main() {
// Here every variable is defined by
// specifying data type to it
string str = "GeeksforGeeks";
int num = 109;
float flo = 12.99;
cout << "I'm a string with value: " << str << "\n";
cout << "I'm a number with value: " << number(num) << "\n";
cout << "I'm a floating point number with value: " << flo;
return 0;
}
import java.util.*;
public class Main {
public static int number(int n) {
return n;
}
public static void main(String[] args) {
// Here every variable is defined by
// specifying data type to it
String str = "GeeksforGeeks";
int num = 109;
float flo = 12.99f;
System.out.println("I'm a string with value: " + str);
System.out.println("I'm a number with value: " + number(num));
System.out.println("I'm a floating point number with value: " + flo);
}
}
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:
#include <iostream>
#include <string>
using namespace std;
int main() {
// Here every variable is defined
// by specifying data type to it
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;
}
// Here every variable is defined
// by specifying data type to it
public class Main {
public static void main(String[] args) {
String str = "GeeksforGeeks";
int num = 109;
float flo = 12.99f;
// This line is incorrect in Java
int num2 = "Welcome to GeekdforGeeks";
System.out.println("I'm a string with value: " + str);
System.out.println("I'm a number with value: " + num);
System.out.println("I'm a floating point number with value: " + flo);
// This line is also incorrect
System.out.println("I'm a number with value: " + num2);
}
}
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:
# Define variables
str_var = 'GeeksforGeeks'
num = 5
flo = 12.99
num2 = 'Welcome to GFG'
# Print statements
print("I'm a string with value: " + str_var)
print("I'm a number with value: " + str(num))
print("I'm a floating point number with value: " + str(flo))
print("I'm a string with value: " + num2)
const str = "GeeksforGeeks";
const num = 5;
const flo = 12.99;
const num2 = "Welcome to GFG";
console.log("I'm a string with value: " + str);
console.log("I'm a number with value: " + num);
console.log("I'm a floating point number with value: " + flo);
console.log("I'm a string with value: " + num2);
Output
I'm a string with value: GeeksforGeeks I'm a number with value: 5 I'm a floating point number with value: 12.99 I'm a string with value: Welcome to GFG