Go to Top

Monday 21 March 2011

Data types in Java

                                   Java provides for two types of datatypes : 
  • Primitive Datatypes.
  • Non-Primitive or Composite Datatypes.
                                  
                                   They are defined as follows : 
  • Primitive Datatypes : These are the fundamental datatypes which are not composed of any other datatype. There are eight kinds of fundamental or primitive datatypes in Java : byte, short, int, long, char, float, double, and boolean.
  1. Integers : These are the integer type numbers. The datatypes byte, short, int, long, are included in this category. Their width and range are as follows : 
          
Data type
Width in Bits
Range
long
64
-9223372036854775808 to 9223372036854775807
int
32
-2147483648 to 2147483647
short
16
-32768 to 32767
byte
8
-128 to 127
       
     2. Floating-Point Types : These are the real numbers. They can contain decimal or fractional parts. The datatypes float and double are included within this category. Their width and range are given below :

Data types
Width in Bits
Range
float
4
3.4 X 10-38 to 3.4 X 1038
double
8
1.7 X 10-308 to 1.7 X 10308

     3. Boolean : it contains the datatype Boolean. This is a special type for representation of true and false values where “0” represents false and “1” represents true.

     4. Characters : The datatype char is included in this group. It can hold any one character such as alphabets or numbers.

                                   Some examples of their usage are given below (only parts of the Java code are given below, not the full program) : 

int a;
a=5;

float b;
b=3.142;

char c;
c='a';
(Note : Character values are to be enclosed within single quotes '')

  • Non-Primitive or Composite Datatypes : These are the datatypes which are composed of the Primitive datatypes. They consist of classes, arrays, and interface. The datatype String is also a Composite datatype. It can hold any character, from alphabets to numbers to special characters and blank spaces. Some examples of their usage are as follows (only parts of the Java code are given below, not the full program) : 

String a;
s="abc123";
(Note : The string values need to be enclosed within double quotes "").

                                   Thus to sum up, you can view the following table :


3 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    java training institutes in chennai |
    java j2ee training institutes in velachery

    ReplyDelete
  2. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting java online training

    ReplyDelete

ShareThis