Variable (programming)
In programming, a variable is made up of a space in the storage system (main memory of a computer) and a symbolic name (an identifier) that is associated with that space. That space contains a known or unknown amount of information, that is, a value. The variable name is the usual way of referring to the stored value: this separation between name and content allows the name to be used independently of the exact information it represents. The identifier, in the computer's source code, may be bound to a value at run time, and the value of the variable may therefore change during the course of program execution. The concept of variables in computation may not directly correspond to the concept of variables in mathematics. The value of a variable in computing is not necessarily part of an equation or formula like in mathematics. In computing, a variable can be used in an iterative process: it can be assigned a value in one place, then used in another, later reassigned a new value, and later used in the same way. Procedures of this type are known by the name of iteration. In computer programming, variables are often given long names to make them relatively descriptive for their use, while variables in mathematics often have short names made up of one or two characters to keep them short on transcription and manipulation.
The space in the storage system can be referred to by different identifiers. This situation is known among English speakers as "aliasing" and could be translated as "on appointment" for Spanish speakers. Assigning a value to a variable using one of the identifiers will change the value that can be accessed through the other identifiers.
Compilers must replace symbolic variable names with the actual location of the data. While a variable's name, type, and location remain fixed, the data stored in the location can be changed during program execution.
Variables can be of length:
- Fix. When the size of the program will not vary throughout the program's execution. All variables, whether of the type of data that are fixed, except for some exceptions — such as collections of other variables (arrays) or strings.
- Variable.- When the size of the same can vary throughout the execution. Typically data collections mostly.
Data types
Since variables contain or point to values of particular types, the operations on them and the domain of their own values are determined by the data type in question. Some types of data used:
- Type of logical data (true/1 or false/0).
- Type of whole data (real, without decimals).
- Type of floating comma (real, decimal).
- Type of character data.
- Type of data chain (text chain(s)).
Variables and passing parameters to subalgorithms
Variables can be exchanged between routines, by value and by reference:
- By value. The value (the data) of the variable is copied in the area of the call stack — locally — corresponding to the new subroutine called. Therefore, this subroutine obtains such value but cannot modify the original variable. This means that if the variable suffers alterations within this routine, in order to access such modifications at the end, it must return the new value of it. If this operation is not performed, the value of the variable will be exactly the same as it had before passing through the function.
- By reference.- The value is not passed directly from the variable, if not a reference or pointer to the same — which contains the address of the memory zone where the content is hosted — so that it is operated directly on the memory zone that contains it, which implies that the modifications that suffer will be accessible later.
Storing variables in memory
Variables are represented by identifiers that refer to a location in program memory where data is stored. A variable is associated with a data type, which, depending on its size, determines the number of bytes that will be necessary to store the variable. In the case of collections and contrary to the rest of data types, whether they are primitives or complex objects, the memory allocated to store such variables is not known in advance, which leads to establishing memory reservation policies:
- Fixed memory reserve.- It predetermines the amount of memory that will be assigned to the collection. It is an extremely rigid policy, since at the end of the memory area new elements could not be stored.
- Variable memory reserve.- A memory zone is dedicated, which may be of a predetermined size or not, and in case of surpassing the memory zone, another zone is assigned, either contiguous or not, to prevent the restriction mentioned above.
Scope
Regarding the scope of a variable, this can be:
- Local: When the same is only accessible from a single fixed procedure, it cannot be read or modified from another brotherly procedure or from the parent procedure itself. It is possible to declare variables in condition blocks, loops, etc so that they can only be accessed in the block itself.
- Global: When the same is accessible both from routines or macros of the application, and in all the procedures and functions of the application.
Although it is true that, in a basic way, the scope of the variables can be defined in the manner explained above, there are degrees of globality of them, and they can be accessible from certain points or others, or even being able to be accessible between different applications, reaching the case of superglobality. Example of the scope of a variable in the Java programming language.
public class A { public Integer Number = new Integer(); /* Global Variable to All Methods */ public Integer Method() { int num = 1; // Variable Local to Method. You can access within this method anywhere, but not outside it. for (int i = 0;i.Number.intValue();i+) { // i is local to the loop for, can only be accessed within it. num ♪ i; ! // i = 2; This line would cause error by not having declared variable i. i was locally defined to the loop for. return Integer.value(num); ! public void other() { int num = 1; // Local variable to anotherMethod. num here is a variable different from the num method variable System.out.println("Variable local num: " + num); ! !
- It must be borne in mind that in Java the case of global variables to all methods found in a class is something peculiar, since these are really attributes which define an object of a particular class, in this case class A has an attribute called an integer. Attribute is a concept that does not define an object of a particular class, but is a variable that supports procedures and conceptually defines objects.
Contenido relacionado
Transport in Spain
Microcomputer
Linux kernel