In this section we will tell you about data type in java. A data type is defined as " type of data that can be stored in variable". Java is a statically-typed language that means before using the variable it must be declared. Declaring a variable includes type of variable and name of variable. By declaring the type of data, compiler restrict the user from assigning a wrong type of data to a variable.
int counter = 1;
By doing so, we are informing compiler that variable "counter" is holding the integer type of data, which has an initial value 1.
There are two data type in java.
Primitive Data type :
Apart from int type there are seven other primitive data types in java programming language. Primitive means which is predefined by the language. 8 primitive data type are as follows:
byte : byte data type is a 8-bit signed two's compliment integer. It has minimum value of -128 and maximum value of 128. It is used when save space in large array. Default value is 0.
short : short data type is 16-bit signed two's compliment integer. It has minimum value of -32768 and maximum value is +32767, it is used to save memory in large array. Default value is 0.
int : int data type is 32-bit signed two's compliment integer. It has minimum value of -2,147,483648 and maximum value is +2,147,483647, it is used to as default data type. Default value is 0.
long : long data type is 64-bit signed two's compliment integer. It has minimum value of -9,223,372,036,854,775,808 and maximum value is +9,223,372,036,854,775,807.Default values is 0, this data type is used when more than int range is required .
float : float data type is single precision 32-bit IEEE 754 floating point. float is mainly used to save memory in array and default value is 0.0f .
double : double data type is a double precision 64-bit IEEE 754 format. this data type is basically used as default data type for decimal. Default value is 0.0d.
boolean : boolean data type has only two possible values true and false. This data type is used for flag that retern true/false. Default value is false.
char : char data type is single 16-bit Unicode character. It has a minimum value of '\u0000' and maximum value of '\uffff'. char data type is used to store any character.
Reference Data type :
It is also called non-primitive or object type, which are used to access object type.
Class, array and interfaces are 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.
Posted on: May 10, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles