A simple casting demo : Data Type cast « Data Type « Java






A simple casting demo

    

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */



/** A simple casting demo. */
public class CastingDemo {
  public static void main(String[] argv) {
    int i, ans;
    double d = 2.75;
    i = d;      // EXPECT COMPILE ERROR
    i = (int)d;    // with cast; i gets 2
    System.out.println("i =" + i);
    ans = (int)d * 3;    // truncate d before multiplying
    System.out.println("ans =" + ans);
    ans = (int)(d * 3);  // multiplies before truncating
    System.out.println("ans =" + ans);
  }
}


           
         
    
    
    
  








Related examples in the same category

1.Cast a float or double to an integral valueCast a float or double to an integral value
2.Casting Demo
3.Get the minimum and maximum value of a primitive data types?
4.Class with methods for type conversion
5.Data type conversion
6.Data type Conversion Util
7.Convert Byte array to Int
8.Convert Number To Target Class
9.Convert byte array to Long
10.Convert long to Bytes
11.Conversion utilities
12.Get Time From Date
13.Get String From Date
14.A method used to build a date for use Marker XML and KML data
15.A method used to build a date for display in line with existing formatting rules