Friday, 13 January 2017

Data Types


Data Type: -

Data type specifies the size and type of values that can be stored in an identifier. The Java language is rich in its types.

Types of Data Type: -

1)         Primitive Data Type: - Which include integer, character, Boolean and floating point.
2)         Non - Primitive Data Type: - Which include classes, string, interfaces and arrays.

Data Type Diagram: -

Data Type Values

Data Type             Size                       Range
byte 8 bit or 1 byte                    − 128 to 127
short 16 bit or 2 bytes −32768 to 32767
int 32 bit or 4 bytes −2,147,483,648 to 2,147,483,647
long 64 bit or 8 bytes 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807
float 32 bit or 4 bytes 3.4e−038 to 3.4e+038
double 64 bit or 8 bytes 1.7e−308 to 1.7e+038

Variable

A variable provide us name storage that our programs can manipulate. The value of Java variable may be change in the program. Java variable uses various types of Data Types such as int, char, float etc
 Declaration: - data type variable name
e.g:- int a

There are three types of variable

1)      Local variable
2)      Instance variable
3)      Class/Static variable
Local variable: -
Local variable is a variable that is declared inside the method and cannot be accessed outside the method is known as local variable.
Example of local variable
class local
{
void a1()
            {
                        int a,b;
                        a = 5;//local variable
                        b = 6;//local variable
                        System.out.println("value of a = "+a);
                        System.out.println("value of b = "+b);
            }
public static void main(String args[])
            {
                        local s1 = new local();
                        s1.a1();
            }
}
Instance variable: -
Instance variable is a variable that is declared inside the class but outside the method is known as Instance variable.
Example of Instance variable.
class instance
{
int a,b,c;//instance variable
void a1()
            {
                        a = 5;
                        b = 6;
            }
void display ()
            {
                        c = a + b;
                        System.out.println("sum = "+c);
            }
public static void main(String args[])
            {
                        instance s1 = new instance();
                        s1.a1();
                        s1.display();
            }
}
Static/class variable: -
Static variable are those variable that is declared static and outside the method. Static variable gets memory when class is created.
Example of static variable
class bank1
{
String name;
static String bank ="J&K Bank";  //static variable
bank1(String n)
            {
                        name = n;
            }
void display ()
            {
                        System.out.println(name+" "+bank);
            }
public static void main(String args[])
            {
                        bank1 s1 = new bank1("Safal");
                        bank1 s2 = new bank1("Sahil");
                        s1.display();
                        s2.display();
            }
}

No comments:

Post a Comment