Saturday, May 18, 2013

Data Types And Initializing the Variables






DATA TYPES:-
JAVA has main four types of Data. They are as follows:-

1) INTEGER:- This group contains byte, short, int and long which represents the whole number.
2)FLOATING-POINT NUMBER:- This group contain float and double which represents the fractional number.
3) CHARACTER:- This group contains char and string which represents symbols in character.
4) BOOLEAN:- This group contains Boolean which represents true / false value.

Width and Range of above data types are shown in following Table:-

NAME
WIDTH
RANGE
Byte
8
–128 to 127
Short
16
–32,768 to 32,767
Int
32
–2,147,483,648 to 2,147,483,647
Long
64
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Float
32
    1.4e-045 to 3.4e+038
Double
64
   4.9e–324 to 1.8e+308

INITIALIZING THE VARIABLES:-
Syntax:-
Type identifier variable_name;
OR
Syntax to initialize the variable except character and string variable:-
Type identifier variable_name = value;
For Character variable:-
Type identifier variable_name = ‘value’;
For String variable:-
Type identifier variable_name = “value”;
Here type identifier means type of data. E.g. int, char, float etc.
e.g.
int x, y, z;                               // declares three integers x, y, and z.
int a = 3, b, c = 5;                 // declares three integers by initializing two of them i.e. a and c.
byte t = 22;                           // initializes t.
double pi = 3.14;                 // declares an approximate value of pi.
char s = 's';                            // the variable s has the value 's'.
float f = 5.26F                       // initializes f. It is important to write “F” to initialize the float value.
Float k = 20.145F                 // initializes k.
String r = “JAVA”;                 // initializes r.

You also can initialize the variable dynamically. Check the following program to clear the idea.

class DynInit
{
public static void main(String args[])
{
double a = 169.0;
double b = Math.sqrt(a);
System.out.println("Square Root is " + c);
}
}

Above program has two variable, a and b. Variable a initialize by constant and variable b is initialize dynamically.
Variable b can be initialize by the computing value of Math.sqrt(a) function.

0 comments:

Post a Comment

    Translate

    Protected by Copyscape