0% found this document useful (0 votes)
12 views

2.2 Data Types in Java

Uploaded by

mrnirajbro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

2.2 Data Types in Java

Uploaded by

mrnirajbro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Primitive Data Types


Primitive data types are the building blocks of data manipulation. These are the most basic
data types available in Java language.

There are 8 types of primitive data types:

1. Boolean data type


2. Integer data type
3. long data type
4. float data type
5. double data type
6. char data type

1. Boolean Data Type


The Boolean data type is used to store only two possible values: true and false. This data type
is used for simple flags that track true/false conditions.

Example:

1. Boolean one = false


2. Integer Data Type
The integer data type is generally used as a default data type for integral values unless
if there is no problem about memory. Its reserved 2 bytes memory location.

Example:

int a = 10, int b = b;


3. Long Data Type
The long data type is a 64-bit integer. Its default value is 0. The long data type is used when
you need a range of values more than those provided by int.

Example:

long a = 100000L, long b = -200000L

4. Floating Data Type


The float data type is a single-precision 32-bit floating point. Its value range is unlimited. It is
recommended to use a float (instead of double) if you need to save memory in large arrays of
floating point numbers. The float data type should never be used for precise values, such as
currency. Its default value is 0.0F.

Example:

1. float f1 = 234.5f

5. Double Data Type


The double data type is a double-precision 64-bit 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.

Example:

double d1 = 12.3
6. Char Data Type
The char data type is a single 16-bit Unicode character.The char data type is used to store
characters.

Example:

1. char letterA = 'A'

Why char uses 2 byte in java and what is \u0000 ?


It is because java uses Unicode system not ASCII code system. The \u0000 is the lowest range
of Unicode system. To get detail explanation about Unicode visit next page.

You might also like