Difference between revisions of "Variable"
|Line 59:||Line 59:|
Revision as of 17:12, 7 June 2019
- 1 Definition
- 2 Relevance
- 3 Explanation
- 4 Resources
In programming, a variable is comprised of:
- a storage location (identified by a memory address)
- an identifier
- value (a known or unknown quantity of information)
As the name variable implies, information may change as the program executes. However, its name, type, and location often remain fixed.
A Compiler will replace a variable's identifier with the data location.
scalar: an alternative term for a variable.
An identifier is the name used to reference either the the stored value or the variable itself; the variable's name can be used separately from the data it represents.
Changing the type of data stored in a variable may change the way the data can be used. For example, in most programming languages two integers added together will produce a sum that is also an integer.
a = 1;
c = a+b; // c will be 3
However, if a is a string (such as "hello"), adding it to an integer would not necessarily provide you with an integer as a result.
a = "hello";
c = a+b; // c will be hello2
There are many types of variables, such as: static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic.
A static variable is also known as global variable, it is bound to a memory cell before execution begins and remains to the same memory cell until termination. A typical example is the static variables in C and C++.
A Stack-dynamic variable is known as local variable, which is bound when the declaration statement is executed, and it is deallocated when the procedure returns. The main examples are local variables in C subprograms and Java methods.
Explicit Heap Dynamic
Explicit Heap-Dynamic variables are nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by the programmer. The main examples are dynamic objects in C++ (via new and delete) and all objects in Java.