Basic Programming Concepts 1: The basics of Variables and Arrays (Updated on July 24th)

Due to the positive feedback I've received on the initial programming writings I've done, I've decided to move forward with the basic programming guide that I have started with.

In computer programs, we can figure out something simple like what 50*49 is. We can also enter in something more detailed such as a bank account information. However, if we cannot store this information, then doing these tasks are essentially worthless. To store and recall information, a variable is used as a reference to the space in memory where the information in question is stored.

There are two types of variables, primitive variables and object variables. The first type of variable is known as primitive, which simple assigns a name to the memory location (or address) which holds the value in memory and be used to recall that piece of information. These types include int for integer, float for floating point numbers (in other words, numbers utilizing decimal points), double for larger floating point numbers, long for larger numbers, and Boolean for true/false logic. To declare a variable, most programming languages will usually use a line of programming such as this.

int number;

The variable that has been declared is called number, and references a space in a system's memory that is held specifically for an integer. This variable in particular is considered to not be initialized as an integer has not been assigned to this variable. To initialize this variable, we assign an integer to this variable in the next example:

int number = 22;

In most programming languages, an equals sign is used as an assignment operator. In addition, a variable can be declared and initialized later in a program. An example of this would be the following:

int number;
number = 22;

Initializing a variable separately can be useful depending on the task that the user wishes to do.

The other type of variable is known as an object variable. An important difference between object variables and primitive variables is that object variables store a reference to an object in the reserved space for that variable and not just the address of the object. An example of object variables is a String value. An example of declaring and initializing a String variable can be seen in the following example:

String name = "Amp";

Another difference with an object variable is that they belong to a class which specifies methods that can be performed on this object. In the case of name, it belong to the String class which specifies what methods can be performed on the object variables that belong to it. An example of a method is shown below where the length of a string is returned:

name.length();

The method I have called is length, which returns an integer stating the length of the name string. In the form I have just used though, the method is not stored or read by anything, so it's best to create a variable that stores this information like this:

int number = name.length();

Now when we output the contents of the number variable, it will state 3 as the length method returns the integer 3. (the size of the string I gave earlier) In later guides, I will go further into detail about creating classes and methods. For now though, this raises a new question. Being able to store one integer is good, but what happens if we need to store more than one integer for a task such as finding the average of a grade? One solution to this is to create an array.

What an array does is in effect, create many variables of the same type. To declare an array with 7 integers, we would do the following:

int[] numArray = new int[7];

Now, before I continue, notice the name I picked for the variable, which I've called numArray. For the variable name, I am using camel casing which means that the first part of the variable is lowercase and every next word in the variable is capitalized. This is a common programming practice from what I have seen and helps differentiate variables from classes. Also notice that I have put brackets after int to symbolize that this is an array. Afterwards, I initialize the array with "new int[7]" which states that we are initializing a new array that is 7 integers, which is also known as the array's length. When an array is initialized, all array values are automatically initialized at 0 for integer variables, null for object variables, and false for a boolean[] array.

To assign a different value to a location in an array, we do the following:

numArray[0] = 4;

In this setup, the brackets are used to state the location in the array that I want to hold a number. When I use 0 for the location, I am actually stating the first location in an array. To access the last location in an array, we would use length - 1, which in this case would be 6. Below are two ways to access the last location in this array:

numArray[6] = 22;
numArray[numArray.length - 1] = 20;

The first example shown simply states location 6. The second location though uses the expression numArray.length to get the length of the array (which would be 7) and subtracts 1 from it to get the last location in the array. The length expression of an array is useful for a variety of tasks and is great when we sometimes don't know how large the array we are using will be.

One very powerful primitive variable that can be used is boolean. Essentially, boolean holds solely true and false which doesn't seem like it can be very useful. However, this primitive variable is regularly used to establish effective loops and if statements and utilizing boolean logic is essential to develop effective programs. More about this will be discussed.