Witscale Test Center

7.3 Literals >  7.3.3 Integral literals


 7.3.3 Integral literals

Whenever you see a whole number typed in a Java source code, it is called an integral literal. Integrals literals can be represented in decimal, octal and hexadecimal number systems. By default, the integral literals are of data type int and they represent the numbers in decimal number system. For example,

 

int a = 28; // valid integral literal of decimal value 28.

 

In order to identify the integral literals in other number systems, you need to have some basic knowledge about them.

 

For a long time, various numbering systems have been in use in the computer industry. You are already familiar with our every day decimal numbering system, which uses 10 digits (0 to 9) to represent numerical values. The binary system uses just 2 (0 and 1). The hexadecimal system uses 16 digits as its name suggest (hexadecimal means sixteen, hex ==>6 and decimal means 10.) and the octal number system uses 8 digits ( 0-7) to represent values.

 

Octal literals

The integral literals are by default decimal values. But you can specifically declare that the value is in octal system by preceding the value with 0 (zero):

 

int aNumber = 034; // valid integral literal of octal value.

     

Octal numbers are represented with 0 to 7 digits. Therefore, there is no single digit above 7 in octal system. The following code will not compile:

 

 int aNumber = 038; // invalid.

 

In octal number system, when you get past the digit 7, you are out of single digits to use. After 7 the next number is 10, 11 and so on till 17. After 17, the next number is 20, 21...and so on. Therefore, the decimal number 8 is represented as 010 as octal literal.

 

Hexadecimal literals

Integral literals can also represent the hexadecimal values. The hexadecimal literal is identified when there is 0x or 0X at the start of the literal. Hexadecimal numbers are represented with 16 digits (0 to 9 and A to F). The 0-9 digits are same as the decimal system. It then uses the alphabetic characters A through F to represent the numbers from 10 through 15.

 

 int aNumber = 0x1A;

 

The hexadecimal literals are case insensitive. This means that you can either use lowercase or uppoercase letters, or even a combination of the two. The following code highlights the hexadecimal literals with a bold face for the same value. All these combinations are valid:

 

 int aNumber = 0x1a;  // valid.

 if(0x1a > 20) {}     // valid

 int aNumber = 0x1A;  // valid.

 int aNumber = 0X1a;  // valid.

 int aNumber = 0X1A;  // valid.

 

The hexadecimal numbers have only the alphabets a through f used as digits. Any other alphabets are invalid. For example,

 

int aNumber = 0Xface; // valid.

int aNumber = 0X2bAd; // valid.

int aNumber = 0Xabag; // Invalid, g is not a valid hex digit.

 

The bit representation of hexadecimal literal is easier to find. Each digit is represented by 4 binary bits. For instance, 0xface has 4 digits. If you know the bit representation of 0 to f, you can find bit representation of 0xface by simply appending them. You also need to make sure the total bits in the bit representation must be equal to the exact bits supported by its type. Since int has 32 bit, the int value 0xface is equivalent to 0000 0000 1111 1010 1100 1110

 

Integral literals of long type

By default, the integral literals are of type int. If you want to define a literal of type long, you need to specify a suffix L(capital case) or l (small case) to the value. For instance:

 

long reallyLongOne = 123456723348L;  // suffix L or l for long literal

long starsInMilkyWay = 400000000000l; 

 

The suffix l (lowercase L) is confusing to read as it looks like a digit 1. Therefore, for better readability, it is recommended to use L as suffix for long literals. Besides the integrals, we also use the fractional values in the code. These values are the floating-point literals, which we will see in the next section.