/**       
 *        CastingExample: Series of example of the
 *        use of casting to convert betwenen the basic varaible
 *        types, namely int to/from double.   
 */

public class CastingExample {
    public static void main(String args[]) {

	double pi = 3.141592653;          // Create double at set to pi
	
	//       When you convert to a data type that ``looses figures''
	//       such as double -> int you MUST ``cast'' 

	int intPart = (int)pi;      // Declare int and set value 
	                            // NOTE (int) is essential.
	System.out.println("pi is : " + pi + " integer part is " + intPart);


	//       Casting ``up'' from say int -> double is optional,
	//       it will occur automatically.
	//       Some books consider it ``good programming style''
	//       I consider it ``un-necessary code''. Its your choice!

	double value = (double)intPart;  // (double) is optional
	                                 // will happen automatically
	System.out.println("Back to double gives " + value);
	

	//        Casting to double is also somethings essentail
	//        for example to prevent int division rules when you
	//        dont want them!
	int top = 22;                     // Create two ints
	int bottom = 7;

	double approxPI = top/bottom;     // Try making approx to pi
                                          // but you get int division.

	System.out.println("Approx to pi with int division is : " +
			  approxPI);

	//         Cast the int to double converts then to double
	//         BEFORE the division, and you get floating point
	//         division.

	approxPI = (double)top/(double)bottom; // Cast to double first

	System.out.println("Approx to pi with cast to double is  : " +
			  approxPI);
    }
}
