Home » Programming » 03 - Names, Bindings and Type Checking
3

Declarations, Type Checking, Type Conversion, Type Compatibility

Declaration is the act of stating how a variable is to be used in a program.

Declarations

Declaration is the act of stating how a variable is to be used in a program. Declarations are statements in a program that serve to communicate to the language translator information about the name and type of data objects needed in the execution of a program. Declaration also indicates the life span of a given element in a program. Declarations can be made implicitly or explicitly. Explicit declaration is deliberately specifying how a variable is to be used. For example, in C, you can declare the variable x to be an integer by stating “int x.” Implicit declaration is when variables assume to be declared even without explicitly declaring it. For example, in Visual Basic, variables are declared to be of type “Variant” even without explicitly declaring it. If you want to declare variables in Visual Basic, the most common that you will use are the Integer, Long, and Single. For integers in the thousands, you use the Integer data type. For integers in the millions, you use the Long data type. For numbers with decimals, you use the Single data type. There are many more data types for you to encounter later on.

Type Checking, Type Conversion, Type Compatibility

A program will only run properly if the data types are coherent with their functionalities within a program. Type checking makes sure that each operation executed by a program receives the proper number of arguments of the proper data type. Static type checking is performed during compilation of the program while dynamic type checking is performed while the program is running. If there is type compatibility or no type mismatch then the program will run smoothly. Otherwise, type conversion has to be done. When there is a type mismatch, a program can simply flag the mismatch as an error or will force a type change called coercion. Coercion is forcing a data type to be converted to the correct data type in order for the program to run. This is also known as implicit type conversion. Explicit type conversion is when the programmer deliberately states a type conversion during the program.

You can try type checking in the Visual Basic Editor you are using. Why don't you try putting in floating point numbers instead of integers or why don't you try putting numbers with decimal places in the variable that you made. See what happens. What kind of type checking was performed?

What if you put in text values instead of numbers in the variable that you made. See what happens? Did type checking occur? Was the program able to run?