![]() |
Data Types In Java |
As we know that before use we have to declare variables inside our class, that variables are also called fields. As per requirement these variable's values can be a number say roll number of a student or name i.e literals of characters. Consider the below line of code.
int rollNum = 10;
Here we are informing the compiler that in our class we are storing roll number of a student inside a variable called rollNum and we are assigning this variable with an initial value 10. This single line can be divided into three parts as below:
- int -- The type of the variable.
- rollNum -- The name of the variable
- = 10 -- Assigning the value 10 to the variable.
the initial value (10) is not mandatory. If no initial value assigned then java will provide default value according to the type.
In java total 8 predefined primitive data types are present and these are all keywords, so we cannot use a variable in our programs. The eight data types are byte, short, int, long, float, double, boolean, char.
All the data types comes with a range and default value and size. See the below table with size, default value and minimum, maximum value these data types can store.
Data
Type |
Size
(in bit) |
Range |
Default
Value |
byte |
8 |
-128 to 127 |
0 |
short |
16 |
-32,768 to 32,767 |
0 |
int |
32 |
-231 to 231-1 |
0 |
long |
64 |
-263 to 263-1 |
0L |
float |
32 |
3.402 x 1038, 1.402 x 10-45 |
0.0f |
double |
64 |
1.7976931348623157 x 10308 , 4.9406564584124654 x 10-324 |
0.0d |
boolean |
1 (This data type represents one bit of information, but its "size" isn't something that's precisely defined) |
--- |
false |
char |
16 |
minimum -'\u0000' (or 0) and maximum '\uffff' (or 65,535 inclusive). |
\u0000 |
Because of this primitive data types java is not 100% object oriented programming language. Java provides wrapper classes to convert these primitives to object. These wrapper classes are present under java.util package.
Primitive Data Types |
Wrapper Class |
byte |
Byte |
int |
Integer |
char |
Character |
short |
Short |
long |
Long |
float |
Float |
double |
Double |
booelan |
Boolean |
Autoboxing : - When automatic type conversion done from primitive to the object of their respective wrapper class, it is called as Autoboxing. e.g. int to Integer, long to Long etc
Unboxing : - The reverse of autoboxing that is from wrapper class object to primitive type conversion is know as Unboxing.
0 Comments