CONVERSIONS AND PROMOTIONS(4)
CONVERSIONS AND PROMOTIONS(4)
Assignment Conversions: assignment conversions occur when the value of an expression is assigned to a variable; the type of the expression must be converted to the type of the variable.
(as always, read the relevant sections of the jls, it’ll do u lots of good.)
public class Loader {
//note constants.
static byte first = 125;
static short second = 126;
static int fourth;
public static void main(String[] args) {
//assignment expression, by value.
fourth = first; //here, a widening primitive
//boxing conversion
Integer boxer = fourth;
System.out.println("value of boxer is: "+boxer);
//take the short constant, do narrowing primitive
//and then box it for an assignment conversion. if
//our short was outside byte range, it'll overflow
Byte firstbyte = (first = (byte)second );
System.out.println("Value of firstbyte is: "+firstbyte);
//therefore we can say the expressions above are
//assignable to the variables on the left by assignment
//conversion or assignment compatible
}
}
We’ve been doing assignment conversions since we encountered variables. This’ easy, right?
Method invocation expression: method invocation expression is applied to each argument value in a method or constructor invocation; the type of the argument expression must be converted to the type of the corresponding parameter.
public class Loader {
//first expression is constant, note.
static byte first = 125;
static short second = 126;
static int fourth;
void addSum(int thevar){
System.out.println("The sum is: "+thevar*2);
}
public static void main(String[] args) {
//method invocation conversion by identity conversion
Loader pupil = new Loader();
//identity conversion
pupil.addSum(fourth);
//widening primitive conversion, short to int
pupil.addSum(second);
}
}
Cough up examples of your own. Exciting!
String conversions: string conversions are effected on operands of binary operations when one of the arguments is a String. The other argument is converted to a String and a new String object, which is the concatenation of the 2 strings is the result of the operator +.
Casting conversion: casting conversion is applied to the operand of a cast operator; the integer of the operand expression must be converted to the type explicitly named by the cast operator.
public class Loader {
static Short i;
static Integer var;
public static void main(String[] args) {
S pss = new S();
T tee = new T();
//cast from S to T correct since S subclass
//of T.
tee = (T)pss;
//type from S to parameterized type T is correct
//but will be checked against erasure.
T
}
}
The principles explained in detail in this section, §5.5 have been touched before except for arrays.
Numeric promotions: numeric promotions are applied to the operands of an arithmetic operator. Identity conversion, widening primitive conversion or an unboxing conversion can be used in numeric promotions.
Usage: used to convert the operands of a numeric operator to a common type so that an operation can be carried out.
Types: a. unary numeric promotion, b. binary numeric promotion.
A. UNARY PROMOTION:
Note:
1. in unary promotion, if the operand is of type Byte, Short, Character or Integer, it is unboxed and then promoted to type int by widening conversion or an identity conversion, else if byte, short, char, promotion to int is immediately carried out.
2. if the operand is of type Long, Float or Double, they are unboxed first and primitive type used. Value set conversion is next applied.
3. else the operand is left as-is.
Examples of expressions where unary promotion can be applied:
public class Loader {
public static void main(String[] args) {
byte b = 2;
int[] a = new int[b];//dimension expression promotion
char c = '\u0001';
a[c] = 1; //index expression promotion
a[0] = -c; //unary promotion
System.out.println("a: "+a[0]+" , "+a[1]);
b = -1;
int i = ~b; //bitwise complement promotion
System.out.println("~0x"+Integer.toHexString(b)
+"==0x"+Integer.toHexString(i));
i = b<<4L; //shift promotion, left operand
System.out.println("0x"+Integer.toHexString(b)
+"<<4L==0x"+Integer.toHexString(i));
}
}
B. BINARY NUMERIC PROMOTION: in binary numeric promotion, for a pair of operands, note that each of the operands must denote a value that is convertible to a numeric type. In binary numeric promotions, the following rules are applied, in order, using widening conversions:
- if any of the operands is of a reference type, unboxing conversion is performed then
- if either operand is of type double, then other is converted to double
- otherwise, if either operand is of type float, the other is converted to float.
- otherwise, if either is of type long, the other is converted to long
- otherwise, both operands are converted to type int.
After type conversion, value set conversion is applied to each operand.
Examples from the jls:
public class Loader {
public static void main(String[] args) {
int i = 0;
float f = 1.0f;
double d = 2.0;
//first int*float is promoted to float then
//float is promoted to double for double==double
if (i*f == d){System.out.println("oops!");}
else {System.out.println("correct maths!");}
//c&b is promoted to int&int
byte b = 0x1f;
char c = 'G'; //numeric value is 7
int control = c & b;
System.out.println(Integer.toHexString(control));
//int:float is promoted to float:float
f = (b==0)? i : 4.0f;
System.out.println(1.0/f);
}
}

No comments:
Post a Comment