Page 145 - Computer science 868 Class 12
P. 145
5.1.1 Primitive Data Types
The primitive data types are independent data types. They specify the size and type of the values of the variables and
act as the building blocks of data which can be manipulated.
There are eight types of in-built primitive data types that are used for coding. They are byte, short, char, int, long,
float, double and boolean. They are also called basic data types.
Let us see all the data types in detail.
Integer Type
Integer data types can hold whole numbers such as 657 and -28. The size of the values that can be stored depends on
the integer type that we choose. The data types under this category are byte, short, int and long.
• byte: The byte data type is an 8-bit signed integer. It can store values between -128 and 127 (both extreme values
are included). The default value of the byte data type is 0.
It is used to save memory in large arrays because a byte is 4 times smaller than an integer. It can also be used in
place of “int” data type.
For example: byte a = 10, byte b = -20
• short: The short data type is a 16-bit signed integer or equivalent to 2 bytes. This is greater than a byte in terms of
size and less than an integer. It ranges from -32,768 to 32767. The default value of the short data type is 0.
For example: short s = 10000, short r = -5000
• int: The int data type is a 32-bit signed integer or equivalent to 4 bytes, which has a minimum value of -2 and a
31
31
maximum value of 2 -1. The latest version of Java can use the int data type to represent an unsigned 32-bit integer,
which has a minimum value of 0 and a maximum value of 2 -1. The default value of the int data type is 0.
32
For example: int a = 100000, int b = -200000
• long: The long data type is a 64-bit integer or equivalent to 8 bytes. The signed long has a minimum value of
63
63
-2 and a maximum value of 2 -1. The latest version of Java can use the long data type to represent an
64
unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2 -1. The default value of the
long data type is 0L.
For example: long a = 100000L, long b = -200000L
Let us tabulate all the above integer data types with their size, value, range and format below.
Type Size Default Range of values that Format
value can be stored
byte 1 byte/8 bits 0 -128 to 127 byte b = 44
short 2 bytes/16 bits 0 -32768 to 32767 short s = 21435
31
31
int 4 bytes/32 bits 0 -2 to 2 -1 int i = 123456876
63
63
long 8 bytes/64 bits 0L -2 to 2 -1 double d = 123456444L
5.1.2 Floating-Point Type
Floating-point data types represent numbers with a fractional part, containing one or more decimals such as 3.4f or
56.2. There are two data types under this category: float and double.
• float: The float data type is a single-precision 32-bit IEEE 754 floating point or equivalent to 4 bytes. The float data
type can store fractional numbers from 3.4e-038 to 3.4e+038, i.e., values up to 7 decimal digits. Note that you
should end the value with an “f”. The default value is 0.0f.
For example: float a = 4.64f, float b = 34.6789f
143
Primitive Values, Wrapper Classes, Types and Casting 143

