Total Pageviews

Tuesday, 21 June 2011

DATA TYPES AND VARIABLES

JAVA is a strongly typed language .
 every declaration made in java that is variables and expressions have  a type and each type is strictly defined. The java compiler checks all the expression and parameters to ensure that type are compatible. Any mismatches an error that must be corrected before the compiler will finish compiling the class.

Primitve data types.
Data types specify the size and the type of data that can be stored. In java , there are eight primitive sata types  -- byte , short , int ,long , char , float , double and boolean.
 This eight primitve data types can be put in four groups.

1. INTEGER : This group consist of  byte , short , int and long.

byte   :    It is the simple integer type.This is signed 8-bit type, that has a range from -128-to-127. This data type are generally used for streams of data from a network or file.They are also useful when we are working with binary data that may not be directly compatible with java's other built-in types.

declaration :: byte b, c;

short : It is signed 16-bit type, and has a  range from -32,768 -to- 32,767. Generally they are not used in java.
declaration ::
short s;
short d;

int : it is signed 32-bit type, that has a range from -2,147,483,648 -to- 2,147,483,647 . Other than mathematical operation ,int type are also used for control loops and index arrays.
note : when the small data types i.e byte and short are operated then they are automatically promoted to int type when the expression is evaluated.

long :
it is signed 64-bit type, and lie in the range from -9,223,372,036,854,775,808 -to- 9,223,372,036,854,775,807. since they hava a wide range, so they are generally used for those programs where we need a large whole number and the requird number doesnt lie in the range of int.
declaration  ::  long XTz

Floating-point numbers : This group includes float and double. 

float : it is used where we require single precision , and occupy 32-bit of storage.It is not used when we require a high degree of precised value.
float HighTemp , LowTemp ;

Double
: it is used where double precision is required  , and uses 64 bit to store the value.
//example
public static void main(String args[])
{
doubloe pi , r, a;
r =10.8;
pi = 3.1416;
a = pi*r*r;
System.out.println("Area of circle is " + a);
}
}

CHARACTERS
It include char type to store character.However, c\c++ programmers beware char in java is not the same as char in c or c++. in c++, char is of 8-bits wide.This is  not the case in java, Instead , java uses unicode to represent character found in all human language , and the range of char is from 0 to 65,536 .There are no negative chars.The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8 bit character set,ISO-Latin-1 ranges from 0 to 255.


//demonstrate char data type

class CharDemo{
 public static void main(String args[]){
char ch1 , ch2;
ch1 = 88;
ch2 = 'y';
System.out.print("ch1 is " + ch1);
System.out.println("ch2 is " + ch2);
}
}

output:
ch1 is x
ch2 is y

explaination :
 in this we have a taken two variables ch1 and ch2 , and declared it as  char type. Now , ch1 is assigned the value 88 and the other variable ch2  is assigned a single character 'y' . The output of ch1 will be the character whose ASCII value is 88 and the output of ch2 is 'y'..


//char variables behave like a integer,
class CharDemo{
public static void main (String args[]);
char ch;
ch='x';
System,out,println("ch contains :" + ch );
ch++;
System.out.printline(" ch is now " + ch);
}}

Variables
The variable is the basic unit of storage in a java program . A variable is defined by the combination of an identifier , a type and an optional initializer. In addition , all variables have a scope , which defines their visiblity , and a lifetime.,

Declaring a variable
In java, all variables must be declared before they are implemented in the program.
declaration :
 type identifier [= value][, identifier [=value]...];
in this way of declaration, we can declare only one data type. example :
int a , b, c;
int d=3,c,e=8; //here d and e are intialized.
byte z = 2;

Dynamic intialization
we can expain it with the help of an example .
//dynamic initalization
   class DynInit {
      double a = 3.0 , b =4.0
      double c = Math.sqrt(a*a + b*b );   // c is dynamically initalized
      System.out.println("Hypotenous = " + c );
    }
  }
In this example , we have declares three local variables a , b and c . a and b are initialized by constants . However c is initalized dynamically to the length of hypotenous.

The scope and lifetime of a variable .
Anything logical code , written within the parenthesis, is known as a block.The block defines the scope of a variable.The variables declared within a block can be accessed within the block only, we can not use it outside the block.
example :
//example
class scope {
   public static void main( String args[]){
   int x;
   x = 10;
   if(x == 10)
{
 int y =100;
 x= y*2;
}
System.out.print(" x = " + x);
}
}
expaination:Here we have declared two variables x and y. the variable x is intialized in the main block whereas , y is assigned within the if block. we cannot use y outside the block but x can be implemented anywhere in the main block.This tells us about the scope of the variables x and y. The output will be // x = 200 //.
The lifetime of a variable is limited to its block. The variables are created at the start of scope and it gets destroyed when the scope is closed.







No comments:

Post a Comment