# Data Type in Java

&#x20;**Datatype** is a special keyword used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.

&#x20;In general, every programming language is containing three categories of data types. They are

&#x20;1\. Fundamental or primitive data types

&#x20;2\. Derived data types

&#x20;3\. User defined data types.

#### &#x20;Primitive data types

&#x20;**Primitive data types** are those whose variables allows us to store only one value but they never allow us to store multiple values of same type. This is a data type whose variable can hold maximum one value at a time.

#### &#x20;Example

&#x20;int a; // valid&#x20;

a=10; // valid a=10, 20, 30; / invalid&#x20;

Here "a" store only one value at a time because it is primitive type variable.

#### &#x20;Derived data types

&#x20;**Derived** data types are those whose variables allow us to **store multiple values of same** type. But they never allow to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In general, derived data type can be achieving using **array.**&#x20;

#### Example

&#x20;int a\[] = {10,20,30}; //&#x20;

valid int b\[] = {100, 'A', "ABC"}; // invalid&#x20;

#### &#x20;User defined data types&#x20;

User defined data types are those which are developed by programmers by making use of appropriate features of the language.&#x20;

User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in java it is achieved using class concept.

&#x20;**Note:** In java both derived and user defined data type combined name as reference data type.

&#x20;In C language, user defined data types can be developed by using struct, union, enum etc. In java programming user defined datatype can be developed by using the features of classes and interfaces.&#x20;

#### Example&#x20;

Student s = new Student();&#x20;

#### Primitive data Type

&#x20;In java we have eight data Primitive type which are organized in four groups. They are

* &#x20;Integer category data types
* &#x20;Character category data types
* &#x20;Float category data types&#x20;
* Boolean category data types

#### &#x20;Integer category data types&#x20;

These category data types are used for storing integer data in the main memory of computer by allocating sufficient amount of memory space.&#x20;

Integer category data types are divided into four types which are given in following table

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTaEpSTV7nTfzKf2rN5%2F-LTaHN1SxzNTJDWjKMHB%2Fimage.png?alt=media\&token=2c0fe950-3a3e-4a63-bcd9-0b3aa8c25a68)

#### &#x20;Character category data types

&#x20;A character is an identifier which is enclosed within single quotes. In java to represent character data, we use a data type called char. This data type takes two bytes since it follows Unicode character set.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTaEpSTV7nTfzKf2rN5%2F-LTaHZelTQ44elZyPvT1%2Fimage.png?alt=media\&token=b8da2f92-9dc7-4f24-aa81-cb5ed7f8c6d1)

#### &#x20;Why Java take 2 byte of memory for store character?

&#x20;Java support more than 18 international languages so java take 2 bytes for characters, because for 18 international languages 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages. Java supports Unicode but c support ascii code. In ascii code only English language are present, so for storing all English latter and symbols 1 byte is sufficient. Unicode character set is one which contains all the characters which are available in 18 international languages and it contains 65536 characters.

#### &#x20;Float category data types

&#x20;Float category data type are used for representing float values. This category contains two data types, they are in the given table

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTaEpSTV7nTfzKf2rN5%2F-LTaI-J79J7QPfRTSjPM%2Fimage.png?alt=media\&token=c7a3045f-b27d-4148-bffe-81e586cac8d1)

#### &#x20;Boolean category data types

&#x20;Boolean category data type is used for representing or storing logical values is true or false. In java programming to represent Boolean values or logical values, we use a data type called Boolean.

#### &#x20;Why Boolean data types take zero byte of memory?&#x20;

Boolean data type takes zero bytes of main memory space because Boolean data type of java implemented by Sun Micro System with a concept of flip - flop. A flip - flop is a general purpose register which stores one bit of information (one for true and zero for false).

&#x20;**Note:** In C, C++ (Turbo) Boolean data type is not available for representing true false values but a true value can be treated as non-zero value and false values can be represented by zero

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTaEpSTV7nTfzKf2rN5%2F-LTaIOJWrNtS7lq1uy1J%2Fimage.png?alt=media\&token=ec103d32-ba3f-40d5-af0f-ca433c90f8bd)

