Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.6.1.2.2 - Narrowing Primitive Conversions

Narrowing conversions are not automatic, they require explicit cast operator. In narrowing conversions, there is a possibility to confront with an overflowing value, with a loss of precision and erroneous results, however if conducted diligently, these conversions may produce useful results.

There is many possibilities for narrowing conversions between different data types. But in general int data type is used generally for integer data and double dataype is used for real values. We wil inspect the result of narrowing conversion between these data types in the following example:

package preliminary;

public class DoubleToInt {

public static void main(String[] args){

 double dblVal = 1.5987651111111E6;
 int intVal = (int)dblVal;// Narrowing Conversion from double to int !
System.out.println("Result = " + intVal);

 } // end main

} //end class DoubleToInt
      The result of this program :
      intVal = 1598765

As it is expected, the decimal part of the data is trimmed after the conversion.

Narrowing conversions are source of trouble, they should be avoided whenever possible. If in need, they should be applied with extreme care and with extreme control of the program output. More information about type conversions can be found in the online documentation, http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)