/*
   This program gives some examples of casting.
*/

public class CastingExamples
{
   public static void main( String[] args )
   {
      int n = 1;
      int m = 3;
      double x = n/m;
      // sometimes you need to cast from a narrower data type to a wider one
      double y = (double)n/m;
      // but you need to be careful how you do the cast
      double z = (double)(n/m);
      System.out.println("x = " + x + " and y = " + y + " and z = " + z );

      // Sometimes it is useful to cast from a wider data type to a narower one
      // as when we generate random integers.
      int randomInt = (int)(6*Math.random()+1);  // roll a 6 sided die
      System.out.println( "The roll of the die produced " + randomInt );

      // When you cast a variable's value from a wider data type to a
      // narrower data type, you can really destroy the value.
      long p = 12345678900L;
      System.out.println( "p as a long is = " + p
                           + " but p cast as an int is = " + (int)p );

   }//main

}//CastingExamples