Data types in java
Data types in java
A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type.
Variable is name of reserved area allocated in memory. In other words, it is a name of memory
location. It is a combination of "vary + able" that means its value can be changed.
1. Primitive data types: The primitive data types are also known as Basic data types/ Built-
in datatypes /Intrinsic data types / Pre-defined data types.
2. Non-primitive data types: The non-primitive data types are know as ‘user defined data
types’.
Primitive data types can be devided into Numerical and Non Numerical data
types.
The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in
place of "int" data type.
2.short
Size= 16 bits (2 bytes) Range = -215 to +215 -1
Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A short data type is
2 times smaller than an integer.
3.int
Size= 32 bits (4 bytes) Range = -231 to +231-1
The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.
4.long
Size= 64 bits (8 bytes) Range = -263 to +263 -1
Its default value is 0. The long data type is used when you need a range of values more than those
provided by int.
1.float
Size=32 bits (4bytes) Range = 1.4e–045 to 3.4e+038
The float data type is a single-precision 32-bit floating point. The float data type should never be
used for precise values, such as currency. Its default value is 0.0F.
2.double
Size=64 bits (8 bytes) Range = 4.9e–324 to 1.8e+308
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double
data type also should never be used for precise values, such as currency. Its default value is 0.0d.
In Java, the data type used to store characters is char. char in Java is not the same as char in C or
C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses ‘Unicode’ to
represent characters. Unicode defines a fully international character set that can represent all of
the characters found in all human languages. For this purpose, it requires 16 bits. Thus, in Java
char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars.
2.boolean
Size= 1 bit Range = either 0 or 1 (0/1)
Java has a primitive type, called boolean, for logical values. It can have only one of two possible
values, true or false. This is the type returned by all relational operators, as in the case of a < b.
boolean is also the type required by the conditional expressions that govern the control statements
such as if and for. Its default value = false