Data types specify the different sizes and values that can be stored in a variable. There are 2 types of data types in Java:
1. Primitive data types – byte, short, int, long, float, double, boolean, char.
2. Non-primitive data types – String, Arrays, Classes, Interface.
1. Primitive data types-
A primitive data types are predefined by the programming language. The size of a primitive type depends on the data type. They do not have additional methods. Their value can never be null and are always written in lowercase.
|byte||1 byte||Store from -128 to 127|
|short||2 bytes||Stores from -32768 to 32767|
|int||4 bytes||Stores from -2147483648 to 2147483647|
|long||8 bytes||Stores from -9223372036854775808 to 9223372036854775807|
|float||4 bytes||Stores fractional numbers having 6 to 7 decimal digits|
|double||8 bytes||Stores fractional numbers having 15 decimal digits|
|boolean||1 bit||Stores true or false|
|char||2 bytes||Stores a single character/letter/ASCII values|
2. Non-Primitive data types-
Non-primitive data types are not predefined in java like primitive data types. They are created by the programmer according to their need and can be null. They are used to store a group of values. The variable of a non-primitive data type is called an object reference variable. Non Primitive types are used to call methods to perform certain operations.
So these were the data types in java. We hope the concept was clear to you. For daily updates and more such content do follow us on Instagram.