Programming basics: what are strings ?

Welcome reader,

When we first talk about, it was mentioned that the String data type were to be discussed in a later post. So what exactly are String ? If you remember, we have the char data type, which is used to store a single character inside a char variable. Sometimes, we need to manipulate more than just one character. What previous programmers did to get around this issue was to use Arrays.

Arrays can really simply your work when you have to store a lot of information into your program. You have to look at arrays like entities in which you can store multiple variables of the same data type in a single structure.

In programming,  an array type is use to describe a collection of elements of the same data type like it was said before.  Each element in the array can be access with identifying keys (indices). Usually, when we have a ten values that we want to store, we start counting from value 1 to value 10. In computer science, we start counting starting from 0.  A quick trick to know the last identifying key of your array would be to take its length and reduce it by one. The length of an array is defined as how many elements it can possibly store.

This is how arrays works :

Array in Java Comparision

Something must be kept in mind while we’re using arrays : they are static. STATIC. Usually, when we use variables, the value that is stored in the variable can be modified at any time. When we initialize an array, we either directly store the values it will hold or we give the maximal length of the array. If ever we were simply initialize the array with its maximal length, every value in the array will either be undefined (null) or 0. Null values will be aborted in a later post. We’ll see below how to do such things.

arrays


//this is how to initialize an array of int

//With a maximal length of 10

int[] anIntArray = new int[10];

//the new operator can be omitted if an initializer is provided

int[] arrayOfNumber = {1,3,5,7,9,11,13,15,17,19};

//once we initialize an array, its maximal length can't be CHANGE !

//but the values that are stored can be modified.

//Storing values in an array can be achieved with the help of their indices

anIntArray[0]=1;

anIntArray[1]=2;

//so on and so on until we reach the last indice

//Storing values can be achieved with the help of loop

//Example with a for loop and using the size (maximal length) of the array

for(int i=0; i<anIntArray.size();i++)

{

anIntArray[i]=(i*2)+3;

//Accessing the indices of the array to store a value, one at a time

}

Arrays have a lot of potential when we are developing a software application. In fact, as it was mentioned before, the programmers of the old had to use char array to store what have become String value. We'll see an example how to create a char array and showing the values of the array for the user.

4

char chABC[26]= {'a','b','c','d','e','f','g','h',i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

for(int i=0;chABC.size();i++){

Console.Write(chABC[i]);

}

Now, we can directly manipulate the String class ( more on classes and null later).  Doing so, we can directly store input from the end-user like a name  or an address. We can also have information which will never change to present to the end-user such as "warning, you are entering a dangerous zone, please make sure you know what you're doing or get out of here."

What's particular with the string data type is the following : it is immutable. That's kind of big word for beginners and it is somehow kind of hard to understand at first. Like arrays, strings cannot be modified. But not in the same way as arrays. When a string data type is initialized, it holds a certain value. If along the way, a programmer was to modify the same string variable, it would not cause any problems. But, it would be as if the programmer created a new string. A string only points to certain memory at a time, if the value is modify, it will never be the same string even though it is the same variable.  After a while, what is called extension methods are used to modify data type that are immutable such as string. This is a more advanced topic and will be discussed at length in a later post. We'll see below an example how of to initialize a string variable

string ourFirstString="This is a string variable. We'll be able to show text with it to end-users of our software application";

Console.WriteLine(ourFirstString);

Kevin

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s