Variables in programming

Variables are memory cells or memory locations that are used to store data during execution.Variable name remains same
 in complete execution.As we know  data within variables may vary ,so they will be created in RAM.Because RAM is a temporary memory and all data will be lost when power goes down.

Variable Declaration
When we specify only name and type of a variable then it is said to be variable declaration. Declaration provides some 
instructions to the compiler or JVM like how much memory a variable will occupy in the RAM.You can declare any variable
using this syntax.

Data_TYPE  VARIABLE_NAME ;
Semicolon is important at the end of line.
Some Variable Declaration Examples:
int city;
int name;
float liters;
char gender;

Variable Initialization
When we specify variable name type and value to the variable then it is said to be variable initialization. Assignment 
operator is used to assign a value to the variable.You can initialize any variable
using this syntax.

Data_TYPE  VARIABLE_NAME = value;
Some Variable initialization Examples:
int price=10;
int total=670;
float liters=4.78;
char gender='m';

Variable types in C , C++ or JAVA
Variable types remains same in most languages, however their declaring or initialization syntax might be changed.Commonly 
their are five type of variable in programming languages,

1)Integer variable
Integer variables can store only numeric data with no decimal points.Both positive and negative values can be stored.
However their values might be limited according to their sub data types.

i) Short Int
Values range: -32767 to 32768

ii) Unsigned Int
Values range: 0 to 65535

iii) Long Int
Values range: -2,147,483,648 to 2,147,483,647

iv) Unsigned Long Int
Values range: 0 to 4294967295


2)Float variable 
Float variables can store numeric values as well as decimal values.That's why these variables are also called floating 
point variables.They can also store positive ans negative values, and their values range is also limited like integer variables.

3) Character Variables
These types of variables are used to store single character , single integer or some symbols like #,@,$.Each
 character will be initialized within the single quotes.

char var='#';
char var1='@';
char male='F';

4) String Variables
String variables are used to store complete name or a complete line on the screen.String must be initialized 
within double quotes.Symbols and numbers can also be used with the string variables.Capital and small letters will
be printed as it is on the screen.String can be initialized in the following way.

String name="JAVA";
String class="Masters 16";
Always write capital S in java and small s in other languages, as we know string is a class in java.String can also be
created in two other way in java.

StringBuilder  variable_name= new StringBuillder("Data goes here");

String  variable_name= new String("Data goes here"); 
 //This is best way in java.
   
5) Array Variables
Array variables can store list of numeric data or string data.In numeric data arrays can also be more then one
 dimensional, like two dimensional arrays for storing matrix data.arrays can be created in the following way.

int numbers[5]={1,2,3,4,5};
String names[3]={"ABC" , "DEF" , "GHI"};
A two dimensional array example
int two_dim[2][3]={{1,2},{3,4},{5,6}};
Reactions

Post a Comment

0 Comments