Data types In Java

By | February 14, 2013
Q) What are data types ?
A)In computer programming, a data type (or datatype) is a classification
identifying one of various types of data .

Data types in Java
Data types in Java 

There are two types of data types in java.

  1.  Primitive data types.
  2.  Reference/Object Data Types.
Java defines eight primitive data types
        byte, short ,int  , long  ,char ,float ,double  and Boolean .
 A primitive type is predefined by the language and is named by a reserved keyword  (like int ,float,….)
Lets discuss each primitive data type 
byte:
  •  Byte is a signed 8 bit type .
  •  It ranges from -128 to 127 .
  •  Byte variables are declared by using byte keyword   Ex: byte b=110 ; 
Short:
  •  Short is a signed 16 bit type .
  •  It ranges from -32,768 to 32,767 .
  •  short variables are declared by using short keyword   Ex short s=1100
int :
  •  int is a signed 32 bit type .
  •  It ranges from -2,147,483,648 to 2,147,483,647 or ( -2^31 to 2^31-1 ) .
  •  int variables are declared by using int keyword   Ex int i=100100
long :
  • long is a signed 64 bit type.
  • It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or (-2^63 to 2^63 -1)
  • long variables are declared by using long keyword  Ex long l=10000L
float:
  • Float data type is a single-precision 32-bit IEEE 754 floating point .
  • It approximate ranges is 4.9e-324 to 1.8e+308 .
  • floating point variables are declared by using float keyword .Ex float f=234.5f
double:
  • double data type is a double-precision 32-bit IEEE 754 floating point .
  • It approximate ranges is 1.4e-045 to 3.4e+038 .
  • double variables are declared by using double keyword  Ex double=100.50
char:
  • char data type is a 16 bit type .
  • It ranges from 0 to 65,536 .
  • char variable are declared by using char keyword Ex char ch=’a’
boolean:
  • boolean data type represents one bit of information.
  • It can have one of the possible values,true or false.
  • boolean variable are declared by using boolean keyword Ex boolean flag=true .

In addition to the eight primitive data types listed above,the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object.
                           for example, String s = “this is a string” .
     A brief about java String variable is discussed in the upcoming article .

Variables:

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Ex int i ;
         for the above declaration of int varible i some amount of memory is reserved to store it  .

There are to types of varibles in java
  1.  Instance variable .
  2.  Local variable .

Instance variable

  • It is defined only inside the class .
  • The default value for uninitialized instance variable is set by compiler.

Locale variable

  • It is defined only inside method ( or function) .
  • The default value for uninitialized local variable is not set by compiler .
  • any attempt to access uninitialized local variable will result in complitation error .

Example
                     class A {
                                       int i;                                    // instance variable
                                     myfunc(){
                                                 int a;                       //local variable
                                                …
                                                …
                                               }
                                }

Default Values for instance variables

    The following chart summarizes the default values for the data types.

         Data Type                        Default Value                   
byte 0
short 0
int 0
long 0L
float 0f
double 0.0d
char ‘u0000’
String null
Boolean false

References

  1. Oracle
  2. Wiki-Java
  3. Wiki-DataTypes