Monday 19 May 2014

Data Types In Java

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
There are two data types available in Java:
  1. Primitive Data Types
  2. Reference/Object Data Types/User Define Data Types
The Primitive Data Types
Java defines eight primitive data  types of data :byte,short,int,long,char,float,double and boolean.The primitive types also commonly known as simple data types. Primitive data types are predefined by the language and named by a keyword.These can be put into four groups:
  • Integers : This group includes byte,short,int and long which are for whole valued signed numbers.
  • Floating-point numbers : This group includes float and double whic represent numbers with fractional precision.
  • Characters : This group includes char, which represents symbols in character set.
  • Boolean : This group includes boolean , which represents only true or false.
Integers :
Java defines four integer types  byte,short,int and long .All of these are signed , positive and negative values.Java dose not  support unsigned , positive integers. 


Name Size Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,483,647
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

byte:

  • The smallest integer type.
  • This is signed 8-bit  type.
  • byte type variables are very useful when working with a stream of data from network or file.
  • Default value is 0.
  • byte variables are declared by byte keyword. E.g. byte b ,  here byte is datatype and b is variable name.


short:
  • short is signed 16-bit type.
  • It is least used java type.
  • Default value is 0
  • short variables are declared by short keyword. E.g short xyz , here short is data type and xyz is variable name.

int : 

  • The most commonly used integer type is int.
  • It is a signed 32-bit type.
  • Default value is 0
  • int variables are declared by int keyword. E.g. int xyz ,  here int is datatype and xyz is variable name.

long : 

  • long is a signed 64-bit type.
  • It is useful where an int type is not large enough to hold the desire value.
  • Default value is 0L.
  • long variables are declared by long keyword. E.g. long xyz ,  here long is datatype and xyz is variable name.
Floating-point Types:
Floating point numbers are also known as the real numbers,and used when evaluating expressions that returns fractional precision.


Name Size Range
float 32 1.4e-045 to 3.4e+038
double 64 4.9e-324 to 1.8e+308

float:

  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • float variables are declared by float keyword. E.g. float xyz ,  here float is datatype and xyz is variable name.
double :
  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Default value is 0.0d.
  • double variables are declared by double keyword. E.g. double xyz ,  here double is datatype and xyz is variable name.
Char : 
  • char data type is a single 16-bit Unicode character.
  • Char data type is used to store any character.
  • char variables are declared by char keyword. E.g. char xyz ,  here char is datatype and xyz is variable name.
boolean :
  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false.
  • boolean variables are declared by boolean keyword. E.g. boolean xyz ,  here boolean is datatype and xyz is variable name.
Reference/Object Data Types:
  • Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Student,Test  etc.
  • Class objects, and various type of array variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or any compatible type.
  • E.g. Test t=new Test();


0 comments :

Post a Comment