Happy Diwali
happy diwali

Saturday, 12 April 2014

Data Types_premitive

Java Data Types  Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables
There are two data types available in Java:
  • Primitive Data Types
  • Reference/Object Data Types 

Primitive Data Types:  There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword.




Sample Program :

public class DataTypesde
{
          public static void main(String[] args)
          {
// int
                   int i=10;
                   System.out.println(i);     //10
                   System.out.println(Integer.MAX_VALUE);         //2147483647
                   System.out.println(Integer.MIN_VALUE);                    //-2147483648
                   System.out.println(Integer.SIZE);                             //32
// double
                   double d=20.9;
                   double d1=20;
          System.out.println(d);                                                      //20.9
          System.out.println(d1);                                                    //20.0
          System.out.println(Double.MAX_VALUE);                //1.7976931348623157E308
          System.out.println(Double.MIN_VALUE);                 //4.9E-324
          System.out.println(Double.SIZE);                            //64
//char
          char c='a';
          char c2='d';
          System.out.println(c);                                                      //a
          System.out.println(c2);                                                    //b
          System.out.println(Character.MAX_CODE_POINT);   //1114111
          System.out.println(Character.MAX_VALUE);           //
          System.out.println(Character.MAX_VALUE);
    System.out.println(Character.SIZE);                              //16
          // string
          String s1="anji";                                                              //anji
          System.out.println(s1);
// float
          float f=10;
          float f2=10;
          System.out.println(Float.MAX_VALUE);                   //3.4028235E38
          System.out.println(Float.MIN_VALUE);           //1.4E-45
          System.out.println(Float.SIZE);                               //32
          System.out.println(f);                                                       //10.0
// byte
          byte b1=10;
          System.out.println(b1);                                           //10
          System.out.println(Byte.SIZE);                      //8
          System.out.println(Byte.MAX_VALUE);          //127
          System.out.println(Byte.MIN_VALUE);            //-128
//short
      short sh=25;
       System.out.println(sh);                                    //25
       System.out.println(Short.MAX_VALUE);  //32767
       System.out.println(Short.MIN_VALUE);   //-32768
       System.out.println(Short.SIZE);              //16
// long
    long t=123;
    System.out.println(t);                               //123
    System.out.println(Long.MAX_VALUE);      //9223372036854775807
    System.out.println(Long.MIN_VALUE);       //-9223372036854775808
    System.out.println(Long.SIZE);                  //64
          }
}

Data types and default size:


Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
happy diwali

Java Varibles

We would see following type of variables in Java:
  • Local Variables
  • Global variables
Local Variables : - A variable that is declared inside the method or block  is called local variable. Local variables within the method or block others methods or block can’t be used .There is no default values
public class localVariables
{
//local variables can be declared within the block
          static
          {
          float f;
System.out.println(f);                                             //CTE
                   f=10;
          System.out.println(f);
          }
// iib block
          {
                   double d;
                   System.out.println(d);                           //CTE
 //local variables can be declared within the method and should be instilized
                   d=10.5;
                   System.out.println(d);
          }
//local variables can be declared within the method
          void method1()
          {
                   int u;
                   System.out.println(u);
//local variables can be declared within the method and should be instilized
                   u=20;
                   System.out.println(u);
//local variables can't be out side
                   System.out.println(i);                 //CTE
                  
          }
// CONSTRUCTOR
          LocalVariables()
          {
                   int t;
                   System.out.println(t);                          //cte
                   t=56;
                   System.out.println(t);
          }
     public static void main(String[] args)
       {
           //object creation
           localVariables v1=new localVariables();
                     int i=10;
           System.out.println(i);                           //10
//local varibles should be instlized
           int j;
           System.out.println(j);                                   //CTE
 //local varibles should be used after the instilization only
           int k;
           int y=k;
           System.out.println(y);               // IILEGAL FORWARD REF
//local varibles any times of reinstilized
           i=20;
           System.out.println(i);
           i=50;
           System.out.println(i);
 //local variables can't be out side
                   system.out.println(u);                         // CTE
      
       }
}

Global variables :
Global variables contains default values its is based on the relevant data types
We can use any where in the class it’s based on type of global variables.
Class Variables :-
  • Class variables are variables declared with in a class, outside any method, with the static keyword.
  • Whenever the class loaded into the memory automatically loaded into static variables before main method
A class can have any number of methods to access the value of various kinds of methods
Default values for static variables  :
public class Defultvalues
{
          int i;
          String s1;
          double d;
          long l;
          float f;
          char c;
          boolean b;
          byte y;
          public static void main(String[] args)
          {
defultvalues v1=new defultvalues();
System.out.println(v1.i);            //0
System.out.println(v1.s1);          //null
System.out.println(v1.d);           //0.0
System.out.println(v1.l);            //0
System.out.println(v1.f);            //0.0
System.out.println(v1.c);           //single space
System.out.println(v1.b);  //false
System.out.println(v1.y);           //0
          }
}

Simple Program :

public class Staticv
{
// static varibles declaration

          static int i;
static
{
          static int h=50;                                             //cte

          System.out.println(i);                                    // cts
}        
          public static void main(String[] args)
          {
          // static varibles can't be used in side main method

          static double d=10;                                        //cte

          System.out.println(i);                                    // 0  
          }
}

Instance Variables (Non-static variables) :
A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
public class Instancevariables
{
// non static varibles
            int i=10;
            double d=10;
            int test1(int i)
            {
                     System.out.println(i);
                                        return i;
            }
           
          public static void main(String[] args)
          {
                   int j=10;
                   Instancevariables ref=new Instancevariables();
      // non static varbiles can be used when ever craeating object
                   System.out.println(i);       // cte if cts i declared as static
                   System.out.println(ref.i);
                   System.out.println(ref.j);  //cte
                   ref.i=20;
                   //test1(ref.j);
//non static members can be calling with ref of object
                   ref.test1(10);
                  
          }

}

Default values: In java default some value will be assigned  in the variables based on data type
public class Defultvalues
{
          int i;
          String s1;
          double d;
          long l;
          float f;
          char c;
          boolean b;
          byte y;
          public static void main(String[] args)
          {
defultvalues v1=new defultvalues();
System.out.println(v1.i);            //0
System.out.println(v1.s1);          //null
System.out.println(v1.d);           //0.0
System.out.println(v1.l);            //0
System.out.println(v1.f);            //0.0
System.out.println(v1.c);           //single space
System.out.println(v1.b);  //false
System.out.println(v1.y);           //0
          }
}
Default values
public class Defultvalues
{
          int i;
          String s1;
          double d;
          long l;
          float f;
          char c;
          boolean b;
          byte y;
          public static void main(String[] args)
          {
defultvalues v1=new defultvalues();
System.out.println(v1.i);            //0
System.out.println(v1.s1);          //null
System.out.println(v1.d);           //0.0
System.out.println(v1.l);            //0
System.out.println(v1.f);            //0.0
System.out.println(v1.c);           //single space
System.out.println(v1.b);  //false
System.out.println(v1.y);           //0
          }
}


happy diwali
happy diwali