CONVERSIONS AND PROMOTIONS(2)
CONVERSIONS AND PROMOTIONS(2)
Kinds of conversions:
1. IDENTITY CONVERSIONS: these are conversions from a type to that same type. Although seemingly trivial, this has 2 practical considerations:
a. to begin with, to ensure desired expression typing
b. implies that programs are permitted to include redundant cast operations for clarity’s sake.
2. WIDENING PRIMITIVE CONVERSION: examples are:
- byte to short, int, long, float or double
- short to int, long, float or double
- char to int, long, float or double
- int to long, float or double
- long to float or double
- float to double
public class WideningPrimitive {
//variables that will engage in widening conversion
static byte first;
short second;
char third;
static int fourth;
long fifth;
static float sixth;
double seventh;
public static void main(String[] args) {
//a.widening primitive conversions do not
//lose info about the overall magnitude of a
//numeric value
first =120;
fourth = first;
System.out.print("from byte to int conversion gives: ");
System.out.println(fourth);
//int to float or double may lose precision in the
//least significant bit
fourth = 1234567890;
sixth = fourth;
//we should have a zero here.
System.out.print
("Subtracting same values though different types
gives: ");
System.out.println(fourth - (int)sixth);
}
}
3. NARROWING PRIMITIVE CONVERSIONS:
- 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.
public class NarrowingPrimitive {
// variables for narrowing conversion
static byte first;
static short second;
static char third;
static int fourth;
static long fifth;
static float sixth;
double seventh;
public static void main(String[] args) {
//narrowing conversion loses magnitude and precision
fifth = +987654321;
second = (short)fifth;
System.out.println("The original, "+fifth+" and the converted, "+second);
third = '\u0098';
second = (short)third;
System.out.print("Narrowing a char, "+third+" to a short "+second);
}
}
4. WIDENING AND NARROWING PRIMITIVE CONVERSIONS:
- byte to char
This involve two steps, a widening conversion to int and then a narrowing to char.
5. WIDENING REFERENCE CONVERSIONS: widening reference conversions exist for any typye S to any type T, provided S is a subtype of T.
6. NARROWING REFERENCE CONVERSIONS: narrowing reference conversions consists of the following:
- from any type S to any reference type T, provided S is a proper supertype of T (special case: the narrowing conversion from the class type Object to any other reference type.)
public class S {}
public class T extends S {}
public class Loader {
public static void main(String[] args) {
S pss = new S();
T tss = new T();
tss = (T)pss; //to prevent run-time error,
//explicit casting done
}
}
- from any class type C to any non-parameterized interface type K, provided C is not final and does not implement K.
public class S {}
public interface K {}
public class Loader {
public static void main(String[] args) {
S pss = new S();
K mokay = (K)pss;
}
}
- from any interface type J to any non-parameterised class type C that is not final.
public class Loader {
public static void main(String[] args) {
//this is the above example other way round
K mokay = null;
S pss = (S)mokay;
}
}
- from the interface type Cloneable or java.io.serializable to any other type T[].
- from any interface type J to any non-parameterized interface type K, provided that J is not a subinterface of K.
public class Loader {
public static void main(String[] args) {
//where K and J are interface types
K mikay = null;
J jeekay = (J)mikay;
}
}
- from any array type S[] to any array type T[] provided that S and T are reference types and there is a narrowing conversion from S to T.
public class Loader {
public static void main(String[] args) {
//where S[] and T[] are arrays of reference types
//S and T.
S[] pss = new S[1];
T[] tee = new T[1];
tee[0] = (T)pss[0]; //first components pls.
}
}

No comments:
Post a Comment