Static typing is a programming language feature in which variables are explicitly declared with a data type, and the type checking is performed at compile time. This means that the type of every variable must be known at compile time, and any type mismatch or error is caught before the program is executed.
In statically typed languages, variables are bound to a specific data type, and this binding is enforced by the compiler. This early type checking helps detect errors and bugs in the code before it is run, leading to more robust and reliable software.
Example in Java: Java is a statically typed language. Here’s an example demonstrating static typing:
int x = 10; // x is explicitly declared as an integer
System.out.println(x); // Outputs: 10
// This would cause a compile-time error
// x = "Hello"; // Attempting to assign a string to an integer variable