Data types in Java
o
In any programming language, we can have
different data types.
o
Data types are basically used to represent the
data in the main memory of the computer by allocating sufficient memory space.
o
In java, we have 3 types of data types. They
are:
o
Fundamental Data Type.
o
Derived Data Type.
o
User Defined / Custom Defined Data Type.
Fundamental Data Types:
In java, we have 8 fundamental data
types which are divided into 4 categories. They are:
o
Integer
Category:
(i)
Byte (ii)
Short (iii) int (iv) long
o
Float
Category:
(i)
Float (ii)
double
o
Character
Category:
(i)
char
o
Boolean
Category:
(i) boolean
Integer Category Data Types:
o
These category data types are used for
representing whole numbers i.e. which don’t contain decimal places.
o
This category contains 4 data types which are
given in the following table:
|
DATA TYPE
|
SIZE (in Bytes)
|
RANGE
|
|
byte
|
1(8 bits)
|
-128 to +127
|
|
short
|
2 (16 bits)
|
-32768 to +32768
|
|
int
|
4 (32 bits)
|
|
|
long
|
8
(64 bits)
|
|
Formula for range calculation: 2n,
where n= number of bits in its size.
·
For byte
data type, n=8 [since 1byte =8bits]
Therefore,
2n
= 28 = 256
256 i.e. 0-255
Now, Divide by
2:
255/2 -
255/2
Since, int
can’t be in decimal-
Therefore
127.5 +0.5 =
-128
And
127.5-0.5 =
+127
Therefore range
is:
-128 to +127
Similarly, other ranges
can be easily calculated.
Float Category Data Types:
o
This category data types are used for
representing the data in the form of (scale, precision) i.e. 13.75
o
This category contains 2 data types which are
given in the following table:
|
DATA TYPE
|
SIZE IN BYTES
|
RANGE
|
|
float
|
4(32 bits)
|
|
|
double
|
8(64 bits)
|
|
Character Category Data Types:
o A character is an identifier
which is enclosed within the single quotes.
o To represent the character
data type in java, we use a data type called ‘char’. This data type occupies 2
bytes since java follows UNICODE character set.
o A UNICODE character set is
one which contains all characters of 18 international languages i.e. Java is
available in all 18 international languages.
o ASCII (American Standard
Code for Information Interchange) contains 256 characters (28) whereas UNICODE (Universal Code) contains 65536
characters (216).
o Java allows us to convert
our java application from one international language to another. The process of
such conversion is known as internationalization.
The tool which is used to perform such conversion is “i18n”.
Boolean Category Data Types:
o Boolean category data types
are used to represent the logical values i.e. true or false.
o In java, to represent the
values true/false, we use a data type called “boolean”.
o This data type does not
occupy any amount of memory space.
Default values
of all category data types:
o Integer Category : 0
o Float Category :
o float : 0.0f
o double : 0.0
o
Character Category : Nothing
o
Boolean Category : False
DERIVED DATA TYPES:
(a) Array
(b) Wrapper Class
Fundamental Data Type Wrapper
Classes
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Wrapper class encloses fundamental data types in
form of object.
USER DEFINED DATA TYPES:
(a)
Class
(b) Interface
No comments:
Post a Comment