Strings, Objects (immutability)

Topic includes

  • immutability
  • literal string
  • methods/functions
  • value type
  • reference type
  • object
  • class
  • verbatim string

Today I’ll discuss what does the heading above mean and why it is important or maybe it isn’t?

When anybody starts to program the first thing that will come up to him are strings. Strings are EVERYTHING in programming, you can do whatever you like with them you can also convert other data type to strings and make some useful operations and then again convert to its primitive type ( I do that very often). At one point you will have to get familiar with the basic methods that strings provide. If you go deeper you will see that you can substitute many string operations using regex.

What does immutable mean?

If you asked a layman what does immutable mean he’ d most probably say something that cannot MUTATE (something that can not change).

I remember when I first heard this word in programming, I immediately thought of a monster who can mutate (can change its appearance, structure, look ect.). Even tough this doesn’t make sense since we want something immutable here.

I must say that I picked up the term really quickly and I understood what it meant but didn’t go deeper with my mind to it.

Enough talking for now, let’s start with an example:

var name="Paul";
name.concat(" and Jenny");
alert(name);

The question is, why doesn’t the literal string “&Jenny” get applied to the name, why doesn’t it change the value of name when the concat() function is applied?? The answer can be found in the title of-course, strings are immutable. This means that the variable “name” is of value type which means that it stores the data directly to the memory and access it as well.

definition of value types (source url:https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx): A data type is a value type if it holds the data within its own memory allocation.

This withdraws the reference types, strings are immutable because the are not reference type rather than that, they are value types as mentioned above.

definition of reference type (source url:https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx): A reference type contains a pointer to another memory location that holds the data.

One thing to clear up here. No matter which type of is the variable “name” the concat() function returns the result “Paul & Jenny” BUT DOES NOT APPLY IT TO THE VARIABLE NAME. It just stays there “hanging in the air”.

example:

var name="Paul";
name.concat(" and Jenny");
alert(name.concat(" and Jenny"));

In the alert box you will see the result “Paul & Jenny” and you will wonder, how did this concatenate now, but if we print out (again showing a alert box) the variable name “Paul & Jenny” the concatenation is not applied and it does not show up? That’s because the function concat() returns the new value and the alert box just display’s it.

To make a conclusion: value types can not be altered/modified/changed. Reference types can be changed.

How do I concat my string literal to the variable string “Paul”?

The easiest solution here would be to override the variable ( replacing the variable name by applying a new content to it, see the following example)

example:

var name="Paul";
name=name.concat("and Jenny");
alert(name);

The output of name will now be “Paul & Jenny”

Using a string like an array is extremely useful sometimes but the immutability kicks in and we have to write some extra code to overcome this state.

example:

 var name="Paul";
 name[0]="r";
 alert(name);

As you can see in the output, the name remains exactly the same (although we’ve “changed” the first letter to ‘r’)By now you should know the answer why this is the case, but, just in case, strings are immutable, strings are value types hence not reference types. Na altering/modifying/changing whatsoever.

Let’s now modify the string in the way that we could not do it before, but first let’s make a small introduction to Objects.

When I started to learn objects (OOP- object oriented programming) things didn’t make sense because I always and I must says once again, always thought in this way: “Why would I use something else if I can achieve that with the previous known stuff”.

Every time someone asks me about objects, I say to him :Objects or OOP programming is used to make things more tidy, more neater.” I didn’t mention more effective, less code or anything else apart from the sentence above.

I always avoided functions/methods, classes ect. because I liked to get my things done quickly just writing the code going step by step, line by line and finding a solution without this fancy stuff which was good until bigger projects came… Then I just new that I had to involve the objects/classes (object!=class) to get my things done quicker!

I’ don’t want to go too deep into objects and classes especially not in the JavaScript sense, since JavaScript does not have a full OOP structure like C# for example, but I will say the most important part of it.

You can think of an object as a wrapper where you wrap inside your properties and functions which you later use by specifying the object name and accessing it’s properties or methods via the . symbol

example:

var person = {
 firstName: "Paul",
 lastName : "Sanders",
 fullName : function() {
 return person.firstName+" "+ person.lastName;
 }
};
alert(person.fullName());

You can see that we have an object which encapsulates (wraps around) properties and methods and we are accessing the method .fullName() via the . symbol (as said) and we are also able to access it because we used the object before “person”.

I will stop here with the explanation of objects, I think that this is more than enough for this post (at the end of the article you can find a short explanation of objects and classes)

Let’s get back now to reference types, why? Because we involved object in the whole story… TO BE CONTINUED…

Incredibly good links on immutability: