Witscale Test Center

7.2 Primitive data types and their ranges


7.2 Primitive data types and their ranges

Primitives are Java’s built-in data types, which mean they are directly understood by the compiler. It means you do not use any class to create (instantiate) them or modify them. Primitives are created directly by specifying a data value; you need not use the new keyword to create them.

Primitive types can be broadly classified as the numeric data types and the boolean datatype. Figure 7.1 shows all primitive types in Java:

 


Figure 7.1 Java primitive data types

 

The numeric primitives can be either signed or unsigned. The signed primitives are further classified as integral primitives and floating-point primitives.  The primitives byte, short, int and long represent whole numbers and hence are called integrals whereas the primitives float and double represent fractional numbers and hence are called floating-point primitives. The primitive char is used to represent characters. It represents them in Unicode encoding as an unsigned numerical value. Therefore, it can also be used as an unsigned datatype. The primitive boolean is used to represent boolean values.

 

The char and String are two unrelated data types in Java. The char is a primitive datatype whereas the String is a class type. The string objects can be created with the new keyword whereas char is not an object but a primitive type whose value can be specified by typing it in the code. 

 

The variables of primitive datatypes hold either numeric or boolean values. The range of values a particular primitive variable holds depends on its type. Java specifies how many bits are required to represent the value of each primitive type. For instance, a primitive variable of type int is stored using 32 bits. Table 7.3 shows all the primitive types and the number of bits Java use to represent them.

 

Table 7.3 Primitive types and their  bit representation

 

Primitive data types

Bit used for representation

boolean

1

byte

8

short

16

char

16

int

32

float

32

long

64

double

64

 

 

The actual memory allocation and size for these datatypes is very much platform dependent. But you need not worry about it. For exam, you need to know the number of bits Java uses to represent them and the ranges of values they can hold.

 

We know now that 32 bits are required to represent an int value. But how can we find the range of values an int variable can hold with these bits? We know that ‘n’ number of bits can represent[§§§§§§§] a total of 2n numbers. If the numbers are unsigned, then n bits can represent numbers in the 0 to (2n – 1) range. The char is an unsigned numeric type and it takes 16 bits for presentation. Therefore, it can hold 0 to (216 –1) unsigned numbers.

For signed numbers, one bit is used as a sign bit. Java represents the signed numbers in 2’s complement form. In this format, the negative numbers have their leftmost bit (the most significant bit or MSB) set as 1. Therefore, the MSB is used to represent the sign of a number. Fgure 7.2 shows the binary representation of byte value 7 for positive and negative numbers.

 

 


Figure 7.2 Binary representation of byte value 7 and –7.

 

In the figure binary representation of +7 and –7 is shown. As you can see, the MSB of bit representation for –7 is 1. You need not worry about knowing how to find a binary representation of a number. However, you must remember that if there is a 1 in the MSB in binary representation, it means it is a negative number.

 

Though details about bit/binary representation of negative numbers are not part of exam, it certainly helps to understand why MSB is 1 in negative numbers. This short example will show you how a negative number of type byte (For instance in byte a = -7;) is represented in binary format. All negative numbers in Java are represented in the 2’s complement form. You can follow these steps to find 2’s Complement of -7.

Take the binary representation of positive value (in this case, byte value 7).

[Representation ==> 0000 0111 ]

Take 1’s Complement of it. The 1’s complement is calculated by inverting the 1s with 0s and 0s with 1s.   [1’s Complement ==> 1111 1000]

Now add 1 to 1’s Complement to get 2’s complement. [2’s Complement ==> 1111 1001]

Therefore, -7 is stored as (1111 1001). If you experiment a little bit you will find that the MSB (most significant bit) of all negative numbers is always 1 in 2’s complement form.

 

 

The byte datatype has 8 bits. Therefore, it can represent 256(28) possible values. Half of them are negative (128). One value is 0 and the rest of them (127) are positive. The positive range is one less than the negative range because the number 0 is represented as a positive number. Therefore, the range of byte is -128 to 127 (which is same as -27 to 27 -1). There is a simple way to find this range for other datatypes. You can use a generic formula for calculating a range of a signed datatype with n bits to represent as:

 

–2(n-1) to 2(n-1) –1 

 

Therefore, the range of byte, which has 8 bits, is –27 to (27–1). Now, you need not remember the exact numbers (-128 to 127). We can find the range of all the signed integral types (byte, short, int, long) using this formula.

 

The range for the floating-point primitives (float and double), which are also signed, is not so easy to determine. You don’t need to know the exact values of largest float and double for the exam. You are expected however to know that the float value is of 32 bits and a double is of 64 bits.

 

The table 7.4 shows the ranges for the values of all primitive datatypes.

 

Table 7.4 Primitive types and ranges of the values they can hold.

 

 

Data Type

Size(in bits)

value-range

 

 

Boolean Datatype

boolean

1

true or false

 

Signed Numeric Datatype

byte

8

-27 to (27-1) or  -128 to +127

 

short

16

-215 to (215-1)

 

int

32

-231 to (231-1)

 

long

64

-263 to (263-1)

 

 

 

float

32

Float.MIN_VALUE[********] to Float.MAX_VALUE

 

double

64

Double.MIN_VALUE to Double.MAX_VALUE

 

Unsigned Numeric Datatype

char

16

0 to (216-1) or  \u0000 to \uFFFF

 

Java has only one unsigned numeric type, char. The char type (a character) contains a bit representation of a single, 16-bit Unicode character. Unicode is a standard encoding used by Java. This standard uses 16 bits to represent a character hence it is capable of representing a wider range of international characters besides the ones represented with the 7-bit ASCII[††††††††] encoding.

 Java also has one boolean datatype, the boolean. It can only take the boolean literals, true or false, as values.  In the next section, we will discuss what a literal is and all kinds of literals in Java code.