Data type: – Every variable in a java has a data type. Data types specify the size and type of values that can be stored. Java language is rich in its data type. The variety of data types available allow the programmer to select the type appropriate to needs of application. Basic data type of java language is int, float, double, byte, char, and string. But data types in java under various categories manly divided into primitive and non-primitive. A tree structure of data type in java are as follows –

Data Type in JAVA.png

Declaration of variable: – In java, variables are the names of storage location. After designing suitable variable name, we must declare them to the compiler. declaration does three things. (1) It tells the compiler what the variable name (2) It specifies what type of data the variable will hold. (3) The place of declaration decides the scope of variables.

A variable must be declare before it is used in the program. A variable can be use the storage a value of any data type. That is, the name has nothing to do with the type. Java allows any property formed variable to have any declare data type. Syntax of the variable declaration is –

                <Data Type> <Variable1, Variable2, _ _ _ >;

Scope of variable: – Java variables are actually classified into three kinds. They are instance variable, class variable and local variable.

Instance variable are created when the objects are initialized and there for they are associated with the object. They tech different values for each object. Class variables, on the other hand, are global to a class and belong to the entire set of object that class create. Only one memory location created for each class. Variables declared and used inside the methods are called local variables. They are called so because they are not available outside the method. Local variables can also be declared inside the program blocks that are defined between the opening and closing curly braces. These variables are visible to the program only from the beginning of its program block to the end of the program block. When the program control leaves a block, all the variables in the block will ceases to exit. The area of the program where the variable is accessible is called scope of variable.

Advertisements