Friday, August 22, 2014

Conversion and Contexts.

CONVERSION AND CONTEXT.

Every expression written in java either has no result or has type that can be deduced.

class Test {
  public static void main(String[] args) {
  // Casting conversion (5.4) of a float literal to
  // type int. Without the cast operator, this would
  // be a compile-time error, because this is a
  // narrowing conversion (5.1.3):
  int i = (int)12.9999f;
  // String conversion (5.4) of i's int value:
  System.out.println("(int)12.5f==" + i);
  // Assignment conversion (5.2) of i's value to type
  // float. This is a widening conversion (5.1.2):
  float f = i;
  // String conversion of f's float value:
  System.out.println("after float widening: " + f);
  // Numeric promotion (5.6) of i's value to type
  // float. This is a binary numeric promotion.
  // After promotion, the operation is float*float:
  System.out.print(f);
  f = f * i;
  // Two string conversions of i and f:
  System.out.println("*" + i + "==" + f);
  // Invocation conversion (5.3) of f's value
  // to type double, needed because the method Math.sin
  // accepts only a double argument:
  double d = Math.sin(f);
  // Two string conversions of f and d:
  System.out.println("Math.sin(" + f + ")==" + d);
  }
}


Output:
(int)12.5f==12
after float widening: 12.0
12.0*12==144.0
Math.sin(144.0)==-0.49102159389846933


Identity conversion:
Conversion from a type to the same type is acceptable.

Widening primitive conversion.
byte to short, int, long, float, double
short to int, long, float, double
char to int, long, float,  double
int to long, float, double
long to float, double
float to double

class Test { 

  public static void main(String[] args) { 
  int big = 1234567890; 
  float approx = big;
  int wide = (int)approx;
  System.out.println("big = " + big + " "+ "approx = " + approx +" "+ "wide = " + wide); 
  System.out.println(big - (int)approx);
 }
}

big = 1234567890 approx = 1.23456794E9 wide = 1234567936
-46

Narrowing primitive conversion:
  short to byte or char
  char to byte or short
  int to byte, short, or char
  long to byte, short, char, or int
  float to byte, short, char, int, or long
  double to byte, short, char, int, long, or float

class Test { 
  public static void main(String[] args) { 
    float fmin = Float.NEGATIVE_INFINITY;
    float fmax = Float.POSITIVE_INFINITY;
    System.out.println("long: " + (long)fmin + ".." + (long)fmax);
    System.out.println("int: " + (int)fmin + ".." + (int)fmax);
    System.out.println("short: " + (short)fmin + ".." + (short)fmax);
    System.out.println("char: " + (int)(char)fmin + ".." + (int)(char)fmax);
    System.out.println("byte: " + (byte)fmin + ".." + (byte)fmax);
 } 
}

Narrow primitive conversion that losses information.
class Test { 

 public static void main(String[] args) { 
 // A narrowing of int to short loses high bits: 
 System.out.println("(short)0x12345678==0x" + Integer.toHexString((short)0x12345678)); 
// An int value too big for byte changes sign and magnitude: 
 System.out.println("(byte)255==" + (byte)255); // A float value too big to fit gives largest int value: 
 System.out.println("(int)1e20f==" + (int)1e20f); // A NaN converted to int yields zero: 
 System.out.println("(int)NaN==" + (int)Float.NaN); 
 // A double value too large for float yields infinity: 
 System.out.println("(float)-1e100==" + (float)-1e100); 
 // A double value too small for float underflows to zero: 
 System.out.println("(float)1e-50==" + (float)1e-50); 
 } 
}


Value set conversion
Assignment conversion.


Assignment context:

primitive assignment conversion.

class Test {
  public static void main(String[] args) { 
    short s = 12; // narrow 12 to short
    float f = s; // widen short to float
    System.out.println("f= " + f); 
    char c = '\u0123';
    long l = c; // widen char to long
    System.out.println("l=0x" + Long.toString(l,16));
    f = 1.23f;
    double d = f; // widen float to double
    System.out.println("d=" + d); 
 }
}

Output
f=12.0
l=0x123
d=1.2300000190734863

Assignment conversion for reference type.

class Point {
  int x, y;
}

class Point3D extends Point {
  int z;
}

interface Colorable {
  void setColor(int color);
}

class ColoredPoint extends Point implements Colorable {
  int color; 
  public void setColor(int color) {
    this.color = color;
  }
}

class Test {
  public static void main(String[] args) {
  // Assignments to variables of class type:
  Point p = new Point();
  p = new Point3D(); // OK because Point3D is a subclass of Point
  //Point3D p3d = p;
  // Error: will require a cast because a Point
  // might not be a Point3D (even though it is,
  // dynamically, in this example.)
  // Assignments to variables of type Object:
  Object o = p; // OK: any object to Object
  int[] a = new int[3];
  Object o2 = a; // OK: an array to Object
  // Assignments to variables of interface type:
  ColoredPoint cp = new ColoredPoint();
  Colorable c = cp;  // OK: ColoredPoint implements Colorable 
  // Assignments to variables of array type: 
  byte[] b = new byte[4]; 
  //a = b; 
  // Error: these are not arrays of the same primitive type 
  Point3D[] p3da = new Point3D[3]; 
  Point[] pa = p3da; // OK: since we can assign a Point3D to a Point 
  //p3da = pa; // Error: (cast needed) since a Point 
  // can't be assigned to a Point3D 
 }
}
----------------------------------------          --------------------------------------          ------------------------------------
class Point {
  int x, y;
}

interface Colorable {
  void setColor(int color);
}

class ColoredPoint extends Point implements Colorable {
  int color;
  public void setColor(int color) {
    this.color = color;
  }
}

class Test {

public static void main(String[] args) { 
  Point p = new Point(); 
  ColoredPoint cp = new ColoredPoint();  // Okay because ColoredPoint is a subclass of Point: 
  p = cp; 
  Colorable c = cp;  // Okay because ColoredPoint implements Colorable:
  // The following cause compile-time errors because 
  // we cannot be sure they will succeed, depending on 
  // the run-time type of p; a run-time check will be 
  // necessary for the needed narrowing conversion and 
  // must be indicated by including a cast: 
  //cp = p; // p might be neither a ColoredPoint 
  // nor a subclass of ColoredPoint 
  //c = p; // p might not implement Colorable 
 } 
}


Assignment conversion for array type.

Casting conversion of reference type.

class Point {
  int x, y;
}

interface Colorable {
  void setColor(int color);
}

class ColoredPoint extends Point implements Colorable {
  int color;
  public void setColor(int color) {
    this.color = color;
  }
}

final class EndPoint extends Point { } 

class Test {
  public static void main(String[] args) { 
  Point p = new Point(); 
  ColoredPoint cp = new ColoredPoint(); 
  Colorable c;  
  // The following may cause errors at run time because 
  // we cannot be sure they will succeed; this possibility 
  // is suggested by the casts: 
  //cp = (ColoredPoint)p; // p might not reference an 
  // object which is a ColoredPoint 
  // or a subclass of ColoredPoint 
  //c = (Colorable)p; // p might not be Colorable 
  // The following are incorrect at compile time because 
  // they can never succeed as explained in the text: 
  //Long l = (Long)p; // compile-time error #1 
  EndPoint e = new EndPoint(); 
  //c = (Colorable)e; // compile-time error #2 
 } 
}

Casting conversion on array type.
class Point {
  int x, y;
  
  Point(int x, int y) {
    this.x = x; this.y = y;
  }
  
  public String toString() {
    return "("+x+","+y+")";
  }
}

interface Colorable {
  void setColor(int color);
}

class ColoredPoint extends Point implements Colorable {
  int color;
  
  ColoredPoint(int x, int y, int color) {
    super(x, y); 
    setColor(color);
  }

  public void setColor(int color) {
    this.color = color;
  }

  public String toString() {
    return super.toString() + "@" + color;
  }
}

class Test { 
  public static void main(String[] args) { 
  Point[] pa = new ColoredPoint[4];
  pa[0] = new ColoredPoint(2, 2, 12);
  pa[1] = new ColoredPoint(4, 5, 24);
  ColoredPoint[] cpa = (ColoredPoint[])pa;
  
  System.out.print("cpa: {");

  for (int i = 0; i < cpa.length; i++)
    System.out.print((i == 0 ? " " : ", ") + cpa[i]);
  System.out.println(" }");
  }
}

Output:
cpa: { (2,2)@12, (4,5)@24, null, null }

No comments:

Post a Comment