10b. Data Types in Java
10b. Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in
Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Java is a statically-typed programming language. It means, all variables must be declared before its use. That
is why we need to declare variable's type and name.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
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.
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.
The int data type is generally used as a default data type for integral values unless if there is no problem about memory.
Example: int a = 100000, int b = -200000