Asynchronous Programming

Asynchronous Programming is another concept which is slowly being used in the desktop and web applications. Have you ever experienced a situation where a web application or a desktop application is slow to respond once you request for something? I bet many of us, might have faced this situation currently or in the past.

This might be because these applications might have not adopted the Asynchronous programming approach.  Let’s take an example  of an Application which does not follow the Asynchronous Programming approach.

Suppose, we have a desktop application which obtains it’s content from the web. And for some reason during some particular day, the internet server is down or it is too slow. Due to this, the whole application will stop functioning and we will not be able to use the other aspects of the application. So, just because one resource fails to execute properly, the whole application stops working. This is not a pleasant situation in case we want the users to continue using our applications.

With Asynchronous Programming, this situation can be handled quite effectively. We can simultaneously run other other parts of the application, in case some aspects stop functioning temporarily.

Let’s take another example. Microsoft Excel is a common application which runs in the Windows Platform and there are similar applications which run on other Operating Systems as well. Many of us have used it.

Now let’s say we have created a spreadsheet regarding Contact Names and Telephone Numbers. And then suppose we want to search the Name of a person. Sometimes even though you are sure the Name is present, it would return a prompt saying that the Name is not found. In this case the search feature of Excel is currently not working. But, we would still be able to add new rows or columns and continue working with our application.

 

C# has recently added a new technique for Asynchronous Programming, to make it easier to adopt this feature.They have added two new keywords Async and Await which in a sense saves time in making the application Asynchronous.

 

 

Type Safety

This is another very important topic in the context of coding. Programming Languages can be classified based on their type safety. Basically you can have two kinds of type safety-

  1. Strongly Typed
  2. Weakly Typed

Some Languages fall into the Strongly Typed Category and some of them fall into the Weakly Typed Category.  Let’s see what to they mean-

A Programming Language is said to be strongly typed if it forces us to define a datatype for each variable. By now, you might be knowing what datatypes are. Some of the common datatypes are int, char, boolean, float etc. It basically defines the type of the data. Whether the data is a number, character or a string.

For Example-

56

This is  a whole number. And so we need to define it as an integer. In order, to use this value we have to store it in a container known as a variable.

Let’s name the variable as “apple”.

We store the value in this variable. And since the contents of the variable is a whole number, we define the variable to be an Integer.

So, we have to write something like this-

int apple=56;

Capture.PNG

We have a pink box (known as a variable) named Apple. And in that box, we store a value called 56.

 

A Strongly typed Language forces us to define the type of a variable. In this case, had we been coding in a strongly typed language, we would have written int apple=56 .

 

Whereas in a weakly typed language, we are not forced to define the type of the variable. We could have simply written apple=56; and it would have worked!

And In a weakly typed language we can mix two unrelated types. By “Unrelated” what I mean is that an Integer and a Character or an Integer and a Boolean Value. Related Types could be Integers and Decimals.

So, in a weakly typed language, we could add a number and a character and it would allow us to do it.

Example

$apple=45;

$ch=”and”;

$next=$apple.$ch;

print $next;

 

This is what we would have written in Perl, which is a weakly typed language. In Perl, the variables are prefixed with a dollar sign. So, we have a variable named apple which stores a whole number 45. And we have another variable named ch which stores a string named “and”. In the third line, we try to combine the two unrelated types. And finally, we try to print that result.

And the result is??

It would be  45and

 

So, this essentially demonstrates that weakly typed languages have no problem mixing unrelated types.

Some of the strongly typed languages are- Java, C# and a  weakly type Language would be Perl.

 

UnBoxing

Today’s topic is in complete contrast to the one I talked about yesterday. Unboxing literally means taking something out of the box. You must have done this quite often! Like unwrapping a gift, opening up a package which you placed an order for.

This is exactly how it works in programming languages. As I had explained earlier, Boxing means converting a value type to a reference type and placing it in the heap portion of the memory.

UnBoxing is just the opposite. Here, The reference type is converted to a value type and it is placed in the stack portion of the memory. The element is unwrapped from the object and it is converted to a value type.

Let’s take an example

Suppose we have a reference type like this-

object o=56;

In order to convert to a value type we have to use the casting operator, something which I would explain in my forthcoming blogs.

int i=(int)o;

So, this basically means we are trying to cast the object to an integer by using the casting operator (int).

So, now you could write-

Console.WriteLine(” “+i);  In C#

or

System.println(”  “+i);  In Java

or

Cout<<” “<<i; In C++

 

All the statements which I listed above do the same thing. They print a value. The only thing is that the syntax is different. Once you try executing any of the statements in the respective languages. The output would be the same, which is 56. And it shows that we have successfully unboxed the element from the heap.

 

 

Boxing

Today we are going to talking about a term known as Boxing. The first thing which would strike your mind once you hear this word is obviously the boxing game. But, what we are going to discuss today has got nothing to do with that.

So, Boxing in programming terminology means, converting a value type to a reference type. I had  explained what Value Type and Reference Type is in my previous blogs. The Value Type is stored in the stack portion of the memory. And once it is converted to a reference type, it is stored in the Heap Portion of the memory.

Let’s see how we can perform boxing on a Value Type. And let’s say we have a value type which is an Integer.

int i=25;

Now, in order to convert this Integer to a reference type, we would need an object.

object 0=i;

And now, the integer is wrapped in an object which can be similar to placing it in the Box and hence the name! This box is now placed in the heap portion of the memory.

 

 

Generics-2

Hi Everyone! In my previous blog I gave you a brief insight of Generics. Today, we will go a little deeper into the topic.

So, the first question which might arise in everyone’s mind is why we need Generics. Let’s see the reasons-

Need of Generics

Some of the common reasons someone would use Generics would be-

  1. It helps to reuse the code.
  2. Helps in Code Maintenance.
  3. It makes us write lesser code.
  4. It improves the efficiency of the program.

 

Let’s say if a person wants to write an algorithm to add two numbers. And assuming that guy doesn’t know generics. He would write two separate functions for adding two numbers. One would be using Integers (Whole Numbers) and the other one would be Floating point numbers (Decimals).

Something like this-

public int Add1 (int a, int b){

int c= a+b;

return c;

}

public float Add2(float a, float b){

float c=a+b;

return c;

}

 

So, the first function named “Add1” is to add two Integers (Whole Numbers) and the second function “Add2”, is to add two floating point numbers (Decimals).

Now, let’s see what would the person have written, had he known Generics

<T> Add(T a, T b){

T c=a+b;

}

 

He would have just written that, and it would have worked for both Integers and Floating Point Numbers. This reduces the number of lines of code drastically and hence improves the efficiency of the program.

Generics

Generics is a concept which has become quite relevant in object oriented programming languages like C# and Java.The name itself,would give you an indication about the topic. It literally means “General”. It is about generalizing objects and methods.

A method  also known as a function, is basically an action which we would like the program to perform. And a method normally takes some arguments as input and returns a value. We define a data type for the input as well as the value which is returned by the function. The data types for most of the programming languages would consist of -Int, Boolean, Float, Byte, Char, Long.

The definition for a method would be something like this-

return type function_name(input type argument_name){}

 Example-

int add(int a, int b){

int c;

c=a+b;

return c;

}

This is a function which takes two values as input and adds them. And it returns the result as an Integer.

If Generics is used in this situation, it would appear something like this-

<T> add(int a, int b){

int c;

c=a+b;

return c;

}

 

If you would notice, we have  replaced int with T. “T” represents any data type and hence we are generalizing it.

Once, we are calling the method, we would write-

<int>add(24,56);

We specify the type only when we are calling the function or method.

One of the main advantages of implementing Generics in your program is it facilitates code reuse. This can be done because we are not explicitly specifying the data type for the arguments of the method.

 

Value Types and Reference Types

This is one topic which you would find in many programming languages. The concept is the same for all only the syntax or the implementation may vary. So, let’s begin with Value Type.

In simple terms, it is a variable which stores a value. The data type of the variables can be the built in data types supported by a programming language or user defined data types. In case of the C# (pronounced as C sharp)  programming language, it would be the struct typed data types. For Example- Int, Boolean, Float, Long, Char, Decimal and Byte.

Whereas reference types are variables which store a reference or an address. Examples of Reference Types would be Classes, Arrays and Strings .

 

Now that you have a basic understanding of what it is, let’s see how they are stored in memory.

Value Types are defined in the Stack  portion of the memory. The life of a Value Type in that memory location is short lived. Once the variable goes out of scope during the execution of the program, it is automatically removed or deleted from the memory.

Reference Types are stored in the Heap portion of the memory. The life of a reference type is longer and it remains in memory till the termination of the program. Unlike Value Types, it is not removed automatically and it needs the assistance of another resource. It is called the Garbage Collector.

Hope you have got some understanding of the two types which we talked about today.