Numeric type conversions with arithmetic operations
For arithmetic operations in which the operands are Short or
Byte boxed types, the result is stored as in
Integer:
var shortVal1:Short = 12
var byteVal1:Byte = 14
var shortVal2:Short = 23
var byteVal2:Byte = 31
var shortSum = shortVal1 + shortVal2
var byteSum = byteVal1 + byteVal2
var comboSum = shortVal1 + byteVal1
The following code tests the resultant types:
print(typeof shortSum)
print(typeof byteSum)
print(typeof comboSum)
Output:
int int int
Similarly, for arithmetic operations in which the operands are short or
byte primitive types, the result is stored as in
int:
var shortVal1:short = 12
var byteVal1:byte = 14
var shortVal2:short = 23
var byteVal2:byte = 31
var shortSum = shortVal1 + shortVal2 // Result is an int
var byteSum = byteVal1 + byteVal2 // Result is an int
var comboSum = shortVal1 + byteVal1 // Result is an int
For arithmetic operations in which one operand is a primitive type and the other is a boxed
version of the same type, the result is stored as the boxed type. For example, the
expression int <operand> Integer returns an Integer,
where <operand> is a +, -,
*, /, or % arithmetic operator. In the
following example, operands of type int and Integer are
added:
var val1:int = 12
var val2:Integer = 14
var sumVall = val1 + val2
var sumVal2 = val2 + val1
The following code tests the resultant types:
print(typeof sumVal1)
print(typeof sumVal2)
Output:
class java.lang.Integer class java.lang.Integer
For arithmetic operations in which one operand is of type float,
Float, double, or Double and the other
operand is of type BigInteger, the result is stored as a
BigDecimal:
var val1:float = 12.8
var val2:double = 14.3
var val3:Float = 3.4
var val4:Double = 52.6
var val5:BigInteger = 31
var sumVa1l = val1 + val5
var sumVal2 = val2 + val5
var sumVal3 = val3 + val5
var sumVal4 = val4 + val5
The following code tests the resultant types:
print(typeof sumVa1l)
print(typeof sumVa12)
print(typeof sumVa13)
print(typeof sumVa14)
Output:
class java.math.BigDecimal class java.math.BigDecimal class java.math.BigDecimal class java.math.BigDecimal
null, a
java.lang.NullPointerException is thrown:var val1:Integer = null
var val2 = 12
var total = val1 + val2 // Throws a null pointer exception
