Chapter 9: Primitive Types.

The Java primitive types include boolean, integer (byte, short, int, and long), floating-point (double and float) and character (char).

The primitive data types are the “building blocks” for Java. Bearing in mind that the computer deals with only 0’s and 1’s, it is necessary to have some means by which information (data) supplied to the computer, can be handled. Hence, these primitive data types are built in to the language. That is, the programmer does not have to create them, but instead just use them (of course, he/she needs to use them appropriately). For example, in mathematics, there isn’t major difference between the numbers 4.0 and 4, but in computers, there is a major difference the hinges upon the way in which these numbers are stored and used. For example, 4.0 would require more space in memory for it to be stored than 4.

The primitive data types are grouped into 2 categories – numeric and boolean. The numeric includes byte, short, long, int, float, double and char. Also, a program can operate on literal values i.e. the actual value being used.

Integer Types:

A variable declared as of type integer could store a value anywhere from 2, 147,483,648 to –2,147,483,647. This is because integer uses 32 bits for its storage. Note: not all 32 bits are used to store the actual number – the leftmost bit is used to store the sign of the number – 0 means that it is a positive number and 1 means that it is a negative number. Thus 231 will give the above mentioned value. Also note that the positive numbers show one more than the negative. This is because of the storage required for 0. If we want to store a number bigger than the above number, then we use the long type which can accommodate a number which would be 263 – 1. And if we still need to store a bigger number, we use the Java built in class BigInteger to declare an object which object we would use to store the huge number. This class is not often used however, and it is advisable to stay away from such big numbers. When very big numbers are used, the program takes longer to execute. The other two types are byte and short. The byte uses 8 bits to store a number and the short uses 16 bits. The byte is hardly ever used. The short however, is often used to declare looping variables and when smaller numbers are handled.

These primitive types in Java are completely defined. That is, there size cannot be changed. For example, in C++, the size of an integer may vary from 16 bits to 32 bits depending upon the computer it is running on. The computer hardware may be built to accommodate (support) integer of size 32 bits instead of 16 bits and vice versa. This is one reason when writing business applications in C++, one of the requirements is that the programmer must know the platform on which the program is to be run. For example, he/she may write the program thinking that 32 bits are available for an integer, but the machine supports only 16 bits per integer. For numbers that require <=15 bits, the application will run smoothly with no problems, thus giving false security. When numbers requiring > 15 bits are to be stored, anything can happen from the program crashes to wrong answers. Not so with Java. The sizes are fixed hence this is one of the reasons that make Java portable. We could use the MIN_VALUE and MAX_VALUE to see what the maximum and minimum values are for each of these types – int.MAX_VALUE will return 2, 147,483,648.

Integer Literals:

We could write integer literals in the following formats in Java – base10 (200), base16 (Hexadecimal - 0xab) or base8 (octal – 025).

Note:

  1. In the case of Octal, the number must begin with a zero (0) and the digits can only

range from 0 – 7.

  1. In the case of Hexadecimal, the number must begin with 0x (zero followed by x) and the digits ranges from 0 – 9 and A – F (either upper or lower case).
  1. We know about the decimal number.

Also a default rule of Java is that a literal is always stored as an int, unless the number ends with a L – example:

10, 24, 2000 are all stored as int.

10L, 24L, 2000L or 2000l (lower case L – but do not use this, it looks like 1) are all

stored as Long.

Converting String to Integers:

We have already used:

Integer.parseInt() //The others are 

Byte.parseByte();

Long.parseLong();

Short.parseShort();

Floating Point Types:

1