Saturday, 9 May 2015

JAVA Primitive Types

 Primitive types are the building blocks of the data portion of a language. Just as matter is composed of atoms clinging together, more complex data types can be made by combining a language's primitive data types. The Java language contains only a small set of primitive types: integer, floating-point, character, and Boolean.
In Java, as in C and C++, you declare a variable by giving its type followed by its name, as in the following examples:
int x;

float LifeRaft;
short people;
long TimeNoSee;
double amountDue, amountPaid;
In the preceding code, xis declared as an int (integer), LifeRaftis declared as a floating-point variable, peopleis declared as a short integer, TimeNoSeeis declared as a long integer, and amountDueand amountPaid are declared as double-precision, floating-point values. 


Floating-Point Types

Support for floating-point numbers in Java is provided through two primitive types-floatand double, which are 32- and 64-bit values, respectively. The operators available for use on these primitives types are shown in Table 3.4.


 Java floating-point numbers will behave as specified in IEEE Standard 754. Java variables of type floatand double can be cast to other numeric types but cannot be cast to be of the booleantype.
If either or both of the operands is a floating-point type, the operation is considered to be a floating-point operation. If either of the operands is a double, then each will be treated as a doublewith the necessary casts being performed. If neither operand is a double, then each operand will be treated as a floatand cast as necessary.
Floating-point numbers can take on any of the following values:
  • Negative infinity
  • Negative, finite values
  • Negative zero
  • Positive zero
  • Positive, finite values
  • Positive infinity
  • NaN, or "not a number"
This last value, NaN, is used to indicate values that do not fit within the scale of negative infinity to positive infinity. For example, the following will produce a value of NaN:
0.0f / 0.0f
The inclusion of NaN as a floating-point value can cause some unusual effects when floating-point values are compared with the relational operators. Because NaN does not fit within the scale of negative infinity through positive infinity, comparing against it will always result in false. For example, both 5.3f > NaNand 5.3f < NaN are false. In fact, when NaN is compared to itself with ==, the result is false.

On the other hand, although negative and positive zero may sound like different values, comparing them with ==will result in true

Other Primitive Types

In addition to the integer and floating-point primitive types, Java includes two additional primitive types-Boolean and character. Variables of type booleancan hold either true or false, while variables of type charcan hold a single Unicode character.

Default Values

One common source of programming errors is the use of an uninitialized variable. Frequently, this type of bug shows itself in a program that behaves erratically. Sometimes the program does what it's supposed to; other times it reformats your hard drive, overwrites your CMOS, declares war on a foreign country, or manifests some other undesirable side effect. It does this because an uninitialized variable may take on the value of whatever random garbage is in its memory location when the program runs. Java circumvents this problem, and possibly prevents World War III, by assigning a default value to any uninitialized variables. Default values are assigned based on the type of the variable, as shown in Table 3.5.


Casting Between Primitive Types

Sometimes you have a variable that is of one type, and you want to use it as another. For example, one of the first programs I wrote was used to predict the final scores in baseball games based on a huge number of input statistics. It would come up with results like the Chicago Cubs beating the San Diego Padres with scores like 3.2 to 2.7. Since it was clearly impossible in real life to score a partial run, the results needed to be converted from floating-point to integer values. This is known as castinga variable. In Java, you can cast a variable of one type to another as follows:
float fRunsScored = 3.2f;

int iRunsScored = (int)fRunsScored;
In this case, the floating-point value 3.2 that is stored in fRunsScoredwill be cast into an integer and placed in iRunsScored. When cast into an integer, the non-whole portion of the fRunsScoredwill be truncated so that iRunsScoredwill equal 3.

This is an example of what is known as a narrowing conversion. A narrowing conversion may lose information about the overall magnitude or precision of a numeric value, as you saw in this case. You should always be careful when writing a narrowing conversion because of this potential for data loss.
The other type of conversion is called a widening conversion. A widening conversion may lose information about precision in the least significant bits of the value, but it will not lose information about the magnitude of the value. In general, widening conversions are much safer. Table 3.6 shows the widening conversions that are possible between Java primitive types.


No comments:

Post a Comment