Difference between revisions of "Working with Variables and Constants in Objective-C"
(New page: In the previous chapter we looked at the basic data types supported by Objective-C. Perhaps the second most basic aspect of programming involves the use of variables and constants. Even th...) |
(No difference)
|
Revision as of 19:48, 5 October 2009
In the previous chapter we looked at the basic data types supported by Objective-C. Perhaps the second most basic aspect of programming involves the use of variables and constants. Even the most advanced and impressive programs, from high end video games to enterprise commerce applications, use variables in one form or another. In this chapter of Objective-C 2.0 Essentials we will cover everything that an Objective-C programmer needs to know about variables.
What is an Objective-C Variable
Variables are essentially locations in computer memory that are reserved for storing the data used by an application. Each variable is given a name by the programmer and assigned a value. The name assigned to the variable may then be used in the Objective-C code to access the value assigned to the variable. This access can involve either reading the value of the variable, or changing the value. It is, of course, the ability to change the value of variables which gives them the name variable.
A variable must be declared as a particular type such as an integer, a character, a float or double. Objective-C is what is known as a strongly typed language in that once a variable has been declared as a particular type it cannot subsequently be changed to a different type. While this may come as a shock to those familiar with loosely typed languages such as Ruby it will be familiar to Java, C, C++ or C# programmers. Whilst it is not possible to change the type of a variable it is possible to disguise the variable as another type under certain circumstances. This involves a concept known as type casting and will be covered later in this chapter.
Variable declarations require a type, a name and, optionally a value assignment. The following example declares an integer variable called interestRate but does not initialize it:
int interestRate;
The following example declares and initializes a variable using the assignment operator (=):
int interestRate = 10;
Similarly, a new value may be assigned to a variable at any point after it has been declared.
double interestRate = 5.5456; //Declare the variable and initialize it to 5.5456 interestRate = 10.98; // variable now equals 10.98 interestRate = 20.87; // variable now equals 20.87
What is an Objective-C Constant?
A constant is similar to a variable in that it provides a named location in memory to store a data value. Constants differ in one significant way in that once a value has been assigned to a constant it cannot subsequently be changed.
Constants are particularly useful if there is a value which is used repeatedly throughout the application code. Rather than use the value each time, it makes the code easier to read if the value is first assigned to a constant which is then referenced in the code. For example, it might not be clear to someone reading your Objective-C code why you used the value 5 in an expression. If, instead of the value 5, you use a constant named interestRate the purpose of the value becomes much clearer. Constants also have the advantage that the if the programmer needs to change a widely used value it only needs to be changed once in the constant declaration and not each time it is referenced.
As with variables, constants have a type, a name and a value. Unlike variables, constants must be initialized at the same time that they are declared and must be prefixed with the const keyword:
const int interestRate = 10;
Once declared, it is not possible to assign a new value to the constant. The following code will cause the Objective-C compiler to report an error that reads "error: assignment of read-only variable":
const int interestRate = 10; interestRate = 5; // invalid attempt to assign new value to read-only const
Note that the value of a constant, unlike a variable, must be assigned at the point it is declared. For example, the following code will not compile:
const int interestRate; interestRate = 10; // invalid attempt to initialize constant after declaration
The above code will, once again, result in a compilation error.