Wednesday, 2 October 2013

Data Types in java

Advertisement

As you all know that data type are the keywords or we can say that it defines the memory assignment  for different "types" available in java.
The data types which are predefined in java is called as keywords.

For ex- int, float, byte etc
On the other side the data types which are defined by user are called as "User defined Data types".
for ex- class, interface etc 
And as per the data representation we can categories it into two types:-
1) Primitive/Basic Data Type.
2) Reference Data Types.

Primitive Data Types

These are the predefined keywords in java.It generally indicates the memory size allocation and types definition for the variables defined in java. If we are defining and primitive data type in our program JVM is going to allocation memory according to the data types defined and then in the allocated memory the actual value will be stored. There are 8 types of primitive data types in java:-

boolean
byte            char              short           int        long
float            double

Boolean :- Boolean in java a one kind of data type which only define the true and false i.e whether this condition is true or not and return only true and false. All the letter should be written in lower case (boolean).
Default value for boolean data type is  " false ".

Note :- It is not going to return 0(false) and 1(true). It is a bit different from  c or c++ .

For ex- if ( 1 ) // invalid in java , but valid in c (1 indicate true in c but in java
                          it is treated as int type )
           if( true ) // valid in java. 

Byte :- Byte is a lower form of int data type. Here lower means, for this JVM is going to allocate less memory i.e 1 byte ( 8 bit ) . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . That means it can store negative (-ve ) value as well. All the letter should be written in lower case (byte).
Range for byte :- -2(-128) to 2 7-1 (127) .

byte b=129; // invalid because it cannot store less than (-128) and greater than (127).
byte b=126; // valid statement because within the range.

Char :- Char is a data type which is used to store single character within single quote ( ' ' ). JVM will allocate  2 byte ( 16 bit ) memory for "char" . Default value for byte data type is ASCII - 0. And we can define it as only unsigned . That means it can store only positive (+ve ) value. All the letter should be written in lower case (char).

Note:- Assigning any char value to a variable means internally the ASCII value for the character is stored in the 2 byte memory space. If we want we can print its ASCII value by type casting that char variable into int type.
Range for char :- 0 to 2 16-1 (65535) .

char c='8' ;  //  valid and will print the value 8.
char c='11'; // Invalid and give error because in the previous example internally it stores
                        the ASCII of 8 , ASCII for numerals are available from (0-9).



Short :-Short is also a lower form of int data type. Here JVM will allocate 2 byte ( 16 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (short).
Range for short :- -215  to 2 15- 1 .

Int :-Int is Integer data type. Here JVM will allocate 4 byte ( 32 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (int).
Range for int :- -231  to 2 31- 1 .

Long :-Long is wider int type. Here JVM will allocate 8 byte ( 64 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (long).We have to append 'l / L' at the end of floating value.
Range for long :- -263  to 2 63- 1 .

Float :-Float is Floating data type that means it can store decimal values. Here JVM will allocate 4 byte ( 32 bit ) memory . Default value for byte data type is " 0.0 ". All the letter should be written in lower case (float).We have to append 'f / F' at the end of floating value.
syntax :- float f = 12.34 f ;
Range for float :- No range because of storing decimal value. We can have infinite decimal value between 0 and 1 so think it has 4 byte memory (32-bit) that means it can have infinite number between these range.

Double :-Double is wider Floating type that means it can store decimal values. Here JVM will allocate 8 byte ( 64 bit ) memory . Default value for byte data type is " 0.0 ". All the letter should be written in lower case (double).
Range for double :-Like floating type it has also no range. 

Reference Data Types

It is the identifier like class, interface/array/string etc in java. If we are defining any reference type then the memory allocated for the reference type is 8 byte always and it will store reference of that reference type rather then any value. But reference is itself in the form of integer value but don't misunderstood that it is storing address for reference type. Address concept in java is abstract.The default value for any reference type is NULL .
Few of the reference type are :-

String , Object, array etc
This is just the introduction to reference type , details of which we will see in their respective topics. 


EmoticonEmoticon