Casting Demo : Data Type cast « Data Type « Java

Casting Demo


/** Casting Demo. */
public class CastNeeded {
  public static void main(String[] argv) {
    int i;
    double j = 2.75;
    i = j;      // EXPECT COMPILE ERROR
    i = (int)j;    // with cast; i gets 2
    System.out.println("i =" + i);
    byte b;
    b = i;      // EXPECT COMPILE ERROR
    b = (byte)i;  // with cast, i gets 2
    System.out.println("b =" + b);


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.A simple 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