CHAPTER 5: Conversions and Promotions Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

5.1 Kinds of Conversion

5.1.1 Identity Conversions , 5.1.2 Widening Primitive Conversions , 5.1.3 Narrowing Primitive Conversions , 5.1.4 Widening Reference Conversions , 5.1.5 Narrowing Reference Conversions , 5.1.6 String Conversions , 5.1.7 Forbidden Conversions

Specific type conversions in Java are divided into six categories.


5.1.1 Identity Conversions

A conversion from a type to that same type is permitted for any type. This may seem trivial, but it has two practical consequences. First, it is always permitted for an expression to have the desired type to begin with, thus allowing the simply stated rule that every expression is subject to conversion, if only a trivial identity conversion. Second, it implies that it is permitted for a program to include redundant cast operators for the sake of clarity.

The only permitted conversion that involves the type boolean is the identity conversion from boolean to boolean .


5.1.2 Widening Primitive Conversions

The following 19 specific conversions on primitive types are called the widening primitive conversions:

Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversion of an int or a long value to float , or of a long value to double , may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (S4.2.4).

A widening conversion of a signed integer value to an integral type T simply sign-extends the two's-complement representation of the integer value to fill the wider format. A widening conversion of a character to an integral type T zero-extends the representation of the character value to fill the wider format.

Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a run-time exception (Chapter 11).

Here is an example of a widening conversion that loses precision:


class Test {
	public static void main(String[] args) {
		int big = 1234567890;
		float approx = big;
		System.out.println(big - (int)approx);
	}
}

which prints:

-46

thus indicating that information was lost during the conversion from type int to type float because values of type float are not precise to nine significant digits.


5.1.3 Narrowing Primitive Conversions

The following 23 specific conversions on primitive types are called the narrowing primitive conversions:

Narrowing conversions may lose information about the overall magnitude of a numeric value and may also lose precision.

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

A narrowing conversion of a character to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though characters represent 16-bit unsigned integer values.

A narrowing conversion of a floating-point number to an integral type T takes two steps:

  1. In the first step, the floating-point number is converted either to a long , if T is long , or to an int , if T is byte , short , char , or int , as follows:
    • If the floating-point number is NaN (S4.2.3), the result of the first step of the conversion is an int or long 0 .
    • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (S4.2.3). Then there are two cases:
      • If T is long , and this integer value can be represented as a long , then the result of the first step is the long value V.
      • Otherwise, if this integer value can be represented as an int , then the result of the first step is the int value V.
    • Otherwise, one of the following two cases must be true:
      • The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long .
      • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long .
  2. In the second step:
    • If T is int or long , the result of the conversion is the result of the first step.
    • If T is byte , char , or short , the result of the conversion is the result of a narrowing conversion to type T (S5.1.3) of the result of the first step.

The example:


class Test {
	public static void main(String[] args) {
		float fmin = Float.NEGATIVE_INFINITY;
		float fmax = Float.POSITIVE_INFINITY;
		System.out.println("long: " + (long)fmin +
								".." + (long)fmax);
		System.out.println("int: " + (int)fmin +
								".." + (int)fmax);
		System.out.println("short: " + (short)fmin +
								".." + (short)fmax);
		System.out.println("char: " + (int)(char)fmin +
								".." + (int)(char)fmax);
		System.out.println("byte: " + (byte)fmin +
								".." + (byte)fmax);
	}
}

produces the output:


long: -9223372036854775808..9223372036854775807
int: -2147483648..2147483647
short: 0..-1
char: 0..65535
byte: 0..-1

The results for char , int , and long are unsurprising, producing the minimum and maximum representable values of the type.

The results for byte and short lose information about the sign and magnitude of the numeric values and also lose precision. The results can be understood by examining the low order bits of the minimum and maximum int. The minimum int is, in hexadecimal, 0x80000000 , and the maximum int is 0x7fffffff . This explains the short results, which are the low 16 bits of these values, namely, 0x0000 and 0xffff ; it explains the char results, which also are the low 16 bits of these values, namely, '\u0000' and '\uffff' ; and it explains the byte results, which are the low 8 bits of these values, namely, 0x00 and 0xff .

A narrowing conversion from double to float behaves in accordance with IEEE 754. The result is correctly rounded using IEEE 754 round-to-nearest mode. A value too small to be represented as a float is converted to positive or negative zero; a value too large to be represented as a float is converted to a (positive or negative) infinity. A double NaN is always converted to a float NaN.

Despite the fact that overflow, underflow, or other loss of information may occur, narrowing conversions among primitive types never result in a run-time exception (Chapter 11).

Here is a small test program that demonstrates a number of narrowing conversions that lose information:


class Test {

	public static void main(String[] args) {

		// A narrowing of int to short loses high bits:
		System.out.println("(short)0x12345678==0x" +
					Integer.toHexString((short)0x12345678));


		// A int value not fitting in byte changes sign and magnitude:
		System.out.println("(byte)255==" + (byte)255);


		// A float value too big to fit gives largest int value:
		System.out.println("(int)1e20f==" + (int)1e20f);


		// A NaN converted to int yields zero:
		System.out.println("(int)NaN==" + (int)Float.NaN);


		// A double value too large for float yields infinity:
		System.out.println("(float)-1e100==" + (float)-1e100);


		// A double value too small for float underflows to zero:
		System.out.println("(float)1e-50==" + (float)1e-50);

	}

}

This test program produces the following output:


(short)0x12345678==0x5678
(byte)255==-1
(int)1e20f==2147483647
(int)NaN==0
(float)-1e100==-Infinity
(float)1e-50==0.0


5.1.4 Widening Reference Conversions

The following conversions are called the widening reference conversions:

Such conversions never require a special action at run time and therefore never throw an exception at run time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.

See Chapter 8 for the detailed specifications for classes, Chapter 9 for interfaces, and Chapter 10 for arrays.


5.1.5 Narrowing Reference Conversions

The following conversions are called the narrowing reference conversions:

Such conversions require a test at run time to find out whether the actual reference value is a legitimate value of the new type. If not, then a ClassCastException is thrown.


5.1.6 String Conversions

There is a string conversion to type String from every other type, including the null type.


5.1.7 Forbidden Conversions



Top© 1996 Sun Microsystems, Inc. All rights reserved.