Friday, January 14, 2011

JAVA Data types and variables

Structure of a method








In JAVA, every variable has a type, every expression  has a type and every type is strictly defined. The java compiler checks all expression and parameters to ensure that the types are compatible. Any type mismatches are errors that must be corrected before the compiler will finish compiling the class. 





The Primitive Data Types

Java defines eight primitive data types. Primitive types are also called as Simple types
These eight data types fall into four groups.

1. Integer -  
  • byte   -    8 bits
  • short -  16 bits 
  • int      -  32 bits
  • long  -  64 bits
2. Floating-point numbers
  • float      -  32 bits
  • double -  64 bits
3. Characters
  • char   -  16 bits
4. Boolean 
  • true or false values  - 1 bits


EX :- for boolean


class BoolTest{

       public static void main (String args[]){
    
               boolean b;
               b = false;
               System.out.println("b is "+b);
               b = true;
               System.out.println("b is "+b);

               if(b){
               
                   System.out.println("This is executed");
              }

             b = false;

              if(b){   
                   System.out.println("This is not executed");
              }

              System.out.println("10>9 "+(10>9));
      }

}




Variables 

The variable is the basic unit of storage in a JAVA program. It is define by combination of an identifiers , a type, and an optional initialize.

Declaring a variable 

In java ,all variables must be declared before they can be used. The basic form of a variable is ;

type  identifier   =  value;

Ex-:
             int height = 10;


Mainly there are 4 types of variables
  • Method local variables
  • Block variables
  • static variables
  • Instance variable

To be continued .........................



-Tharindu Dilshan-

1 comment: