Wednesday, 19 November 2014

Benefits Of Data Types. P4

In this blog I will explain the different data types and how they are used. All their applications and their advantage. But first I will say a bit about data types. For programmers, it is important to know about data types. This is so the program runs more efficiently.

I will explain 6 different data types (In terms of java)

1) String
2) short
3) char
4) int
5) double
6) byte


String:

A string is used to store characters. When I say "characters" I mean spaces, numbers, alphabets, or symbols. It could be anything on the keyboard. It is used to mainly display messages and store messages as well. An example can be

String sMyName = "Muhammad Shaeel Abbas";
This stores "Muhammad Shaeel Abbas" in the variable called "sMyName"

int:

"int" is the short form of "Integer". It is used to store numbers. To be more specific, it is used to store whole numbers. It should only be used when there are very huge values being involved in your program. Whole numbers are numbers without a decimal point within them. There is a specific range for integers as they have their limits. The range is from -2,147,483,648 to 2,147,483,647. It stores numbers as :

int iYourAge = 18;


byte:

The smallest unit for storing integers can be easily identified as "byte". It has a range and that range is from -128 to 127. For the program to run more efficiently, bytes should be used by programmers where it is applicable.

Such as
byte bTempOutisde= 20;

char:

The full form of "char" is character. It is a data type. In computing terminology, a character is a single letter,number, symbol or space. This data type can be used as demonstrated below

char cFirstInitial="A";

double: 

This data type is a bit similar to integer. However it is used for decimal numbers such as "9.1"  "2.3" whereas integers can not be applied to decimals and are used for whole numbers such as "9"  "1"  "23". If we take a look at the range of double, it is 4.94065645841246544e-324d to 1.79769313486231570e+308d

It can be used as:

double dHeightInFeet= 6.1;


short:

"short" can be compared to byte. It can be noticed that short is much bigger than byte as the range for short is -32,768 to 32,767. The limitations of "short" is that it can not hold huge numbers like integer can however it can hold larger numbers than byte. It can be used as such

short sDateOfBirth= 1996; 


As you can see, each and every data type has its own unique purpose to serve to the programmer. It is better to use data types where they are meant o be used rather than mixing them up (Especially numberical data types). If we mix them up then we are wasting energy while coding, and time and also at the end the program not even work. I can give you can example. Imagine having to use "char" instead of "String". How hectic would that be.

No comments:

Post a Comment