Thursday 21 November 2013

What is Variables?

By on 09:16

Variable:-
In programming, a variable is basically a container used to hold data. The data itself can be anything from a simple number to the lyrics and metadata about a song.When we create a variable in Object-c ,we usually specify what kind of data it will hold.This is known as statically typing data.Block of memory in computer are allocated as the "container" to hold the data,and we store a reference to that container with a name that we provide.
"static typing" means that the type of data each variable (container) will hold is declared up front.So for example, if a variable is supposed to hold a  number, we need to say so, and we want be allowed to put something else like a letter in it.Variables that are statically type will be checked before the program even runs,and well be presented with an error if we forget to declare a type of data or declare the wrong one.
So,let's finally take at some code,here an example of declaring variable and assigning some data to it.



The line above is a statement that declares a variable named number that hold   int(integer number) data.we then assign this variable the value 24. In essence ,it puts the value 24 in the container named number.Later in code we can use the container's name to get the data we put inside it.

 1. The first word in a variable declaration is the data type, which tells us what kind of data the variable will hold.
 2   The second word is the name of the variable, which can be anything you want following a few conventions. Variable names must not contain any spaces or special characters; they can only have letters, numbers, and underscores. They must not start with a number, though.
3. The equals sign (=) is an operator, meaning it performs a specific operation for us. This is the assignment operator, meaning that we use it to assign values to variables. In this example it is assigning the integer value on the right (#4) to the variable number on the left (#2).
4. The number in green is the int value we are working with. int numbers are positive or negative whole numbers with no decimal parts, just like this example.
5. Finally, the last character in this line is the semicolon, which is used to finish this statement. Semicolons in Objective-C are like periods in sentences: we use them to indicate when were done saying something. Every statement in Objective-C must end with a semicolon (note that a statement may be displayed on multiple lines for better readability).
---
A Real World Example of Variables:-
Imagine a very large warehouse with lots of storage bays, tables, shelves, special rooms etc. These are all places where you can store something. Let’s imagine we have a crate of beer in the warehouse. Where exactly is it located?
We wouldn't say that it is stored 31' 2" from the west wall and 27' 8" from the north wall. In programming terms we also wouldn't say that my total salary paid this year is stored in four bytes starting at location 123,476,542,732 in RAM.
---

Data in a PC:

The computer will place variables in different locations each time our program is run. However our program knows exactly where the data is located. We do this by creating a variable to refer to it and then let the compiler handle all the messy details about where it is actually located. It is far more important to us to know what type of data we will be storing in the location.
In our warehouse, our crate might be on section 5 of shelf 3 in the drinks area. In the PC, the program will know exactly where its variables are located.
---

Variables are Temporary:-

They exist just as long as they are needed and are then disposed of. Another analogy is that variables are like numbers in a calculator. As soon as you hit the clear or power off buttons, the display numbers are lost.
---

How big is a Variable?

As big as is needed and no more. The smallest a variable can be is one bit and the largest is millions of bytes. Current processors handle data in chunks of 4 or 8 bytes at a time (32 and 64 bit CPUs), so the bigger the variable, the longer it will take to read or write it. The size of the variable depends upon its type.
---

What is a Variable Type?

In modern programing language, variables are declared to be of a type.
Apart from numbers, the CPU does not make any kind of distinction about the data in its memory. It treats it as a collection of bytes. Modern CPUs can usually handle both integer and floating point arithmetic in hardware. The compiler has to generate different machine language instructions for each type, so knowing what the type of variable helps it generate optimal code.
---

What Types of Data Can a Variable Hold?

The fundamental types are these four.

1.   Integers (both signed and unsigned) 1, 2,4 or 8 bytes in size. Usually referred to as int.
2.   Floating Point Numbers up to 8 bytes in size.
3.   Bytes. These are organized in 4s or 8s (32 or 64 bits) and read in and out of the CPUs registers.

4.   Text strings, up to billions of bytes in size. CPUs have special instructions for searching through large blocks of bytes in memory. This is very handy for text operations.
5.   There is also a general variable type, often used in scripting languages.
6.   Variant - This can hold any type but is slower to use.
---

Where is Variables Stored?

1.   In memory but in different ways, depending on how they are used.
2.   Globally. All parts of the program can access and change the value. This is how older languages like Basic and FORTRAN used to handle data and it is not considered a good thing. Modern languages tend to discourage global storage though it is still possible.
3.   On the heap. This is the name for the main area used. In C and C++, access to this is via Pointer variables.
4.   On the stack. The stack is a block of memory that is used to store parameters passed into functions, and variables that exist local to functions.
---





















0 comments:

Post a Comment