Friday 1 March 2013

Datatypes


Datatype: Datatypes are used to specify the type of data that a variable can attain.  There are 2 types of datatypes.  1. Primitive Datatypes 2. Non-Primitive Datatypes
1. Primitive Datatype: It is also called as standard, intrinsic or built-in datatypes.  There are eight primitive datatypes available in Java.  Those are byte, short, int, long, char, float, double and boolean.  These can be classified into four groups.
            Integers                                   :           byte, short, int, long
            Floating point numbers           :           float, double
            Characters                               :           char
            Boolean                                   :           boolean

Type                    Size                                                 Range
                       Bits   Bytes

            int                   32        4                      -2,147,483,648 to 2,147,483,647
            long                 64        8                      -9,223,372,036,854,775,808         to
                                                                         9,223,372,036,854,775,807
            short                16        2                      -32,768 to 32,767
            byte                 8          1                      -128 to 127
            float                 32        4                      3.4 e –0.38 to 3.4 e +0.38
            double             64        8                      1.7 e –308 to 1.7 e +308
            char                 16        2                      0 to 65535
            boolean            8          1                      true / false

Note:   1. Java does not support have any unsigned types
            2. Variables of types char, byte, short, int, long, float and double are all given the value ‘0’ by default
            3. Variable of type boolean are given ‘false’ by default
            4. All primitive datatypes in Java are portable (Platform independent)
2. Non-Primitive Datatype: These are also known as Derived Datatype or Abstract Datatype or Reference type.  These are built on primitive data types.
Examples: string, class, array, interface etc.,

Variable: The name given to the various elements to store the information is called an identifier or variable.  The rules for variables are as follows:
Ø  Variable must be begin with a letter, a dollar ($) symbol or an underscore ( _ ) followed by a sequence of letters
Ø  Variable names must be unique
Ø  There should be no space in between any two characters of an identifiers
Ø  Keywords cannot be used for variable name
Ø  Java is a case-sensitive, so upper and lower case letters are distinct

Declaring a Variable: All variables must be declared before they can be used.  The basic form of a variable declaration is
            Syntax:            type identifier;
Where type is represents datatype of the variable, identifier is the name of the variable.
            Example:         int a;
More than one variable of the same type can be declared by using a comma-separated list.
            Syntax:            type identifier1, identifier2, ……, identifiern;
            Example:         float x, y, z;

Initializing a Variable: Variable can also be initialized with equal sign and a value.
            Syntax:            type identifier = value;
            Example:         int a = 3;

Dynamic Initialization: Java allows variables to be initialized dynamically, using any expression valid at a time the variable is declared.
            Example:         class dyna
                                    {
                                                public static void main(String args[])
                                                {
                                                            double a=3.0, b=4.0;
                                                            double c=Math.sqrt(a*a+b*b);
                                                            System.out.println(“Hypotenuse is” + c);
                                                }
                                    }

Scope & Lift-time of a Variable:

            An identifier duration also called lifetime is the period during which that identifier exist in memory.  Identifier that represent local variables in a method i.e., parameters and variables declared in the method body have automatic duration.  Automatic declaration are created when program control reaches their declaration they exist while the block in which they are declare is active and they are destroyed when the block in which they are declared is exceeded.  We will refer to variables of automatic duration has automatic variables or local variables.
            Java also has identifiers of static duration.  Variables and references of static duration exist from the point at which the class that defines them is loaded into memory for execution until the program termination.

            Identifier scope is where the identifier can be a reference in a program.  The scope of an identifier is class scope and block scope.
            Methods and instance variable of a class have class scope.  Class scope begins at the beginning left brace of the class definition and ends at the closing right brace of the class definition.
            Identifiers declared inside a block have block scope.  Block scope begins at the identifiers declaration and ends at the terminating right brace of the block.  Local variables of the method have block scope.

No comments:

Post a Comment