









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the primitive data types in Java, including their names, default sizes, and value ranges. Learn about boolean, char, byte, short, int, long, float, and double data types.
Typology: Summaries
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Data Types- 1
In java, every variable should have some type, which is called data type, which specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
There are 8 types of primitive data types:
Data Type Default size boolean 1 bit char 2 byte byte 1 byte short 2 byte int 4 byte long 8 byte float 4 byte double 8 byte
The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65, inclusive).The char data type is used to store characters. Example: char letterA = 'A'
The short data type is a 16-bit signed two's complement integer. Its value-range lies between - 32,768 to 32,767 (inclusive). 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. Example: short s = 10000, short r = - 5000
The long data type is a 64-bit two's complement integer. Its value-range lies between - 9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 - 1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. 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
The byte data type is an example of primitive data type. It is a 8-bit signed two's complement integer. Its value-range lies between - 128 to 127 (inclusive). Its minimum value is - 128 and maximum value is 127. Its default value is 0. 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. Example: byte a = 10, byte b = - 20
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.
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. Unicode is a universal international standard character encoding that is capable of representing most of the world's written languages.
Solution To solve these problems, a new language standard was developed i.e. Unicode System. In Unicode, character holds 2 byte, so java also uses 2 byte for characters. lowest value: \u highest value: \uFFFF