Declaring Variables and Constants

Declaring variables and constants, in its simplest form, means that you provide a statement that creates the named memory space.

The exact format varies depending on the language you are using.

Variables

Active Server Pages, VBScript, and JavaScript use what are called variants. What this means is that the variable is declared, but is not assigned a specific data type. The data type depends on the value you assign to the variable. If you assign a number, it becomes a number data type. If you assign a string, it becomes a string data type.

ASP and VBScript JavaScript
Dim FirstName var firstName;

Visual Basic, Java, Java Server Pages, and C++ are known as strongly typed languages. That means you must define what the data type is at the same time you declare the variable. Also, you tend to see scope as part of the declaration:

Visual Basic Java, JSP, C++
Dim FirstName as String
Public JobTitle as String
Private StartDate as Date
String FirstName;
Public String JobTitle;
Private Date StartDate;
 

One additional thing you can do with .Net, Java, JSP, and C++ is initialize the value of the variable when it is declared

Visual Basic Java, JSP, C++
Dim FirstName as String = "Joe"
Public JobTitle as String = "Computer Programmer"
String FirstName = "Joe";
Public String JobTitle = "Computer Programmer";

 

Constants

These are handled very differently

Visual Basic, ASP, VBScript Java, C++
Const PI As Double = 3.1415926 Final Double PI = 3.1415926

 

Determining the Data Type of a Variable

For Visual Basic, use the TypeName function:

TypeName(VariableName)
ThisVariableType = TypeName(FirstName)

For Java, use the instanceof comparison operator:

variableName instanceof dataType
firstName instanceof String