- Forward


Type Conversion
Implicit and Explicit


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

A Common Approach to Type Matching
Back SMYC Forward
  • The Principle:
    • "Each data operation must involve a single data type."
    • "Any operation involving values of more than one data type" ... "is considered to be a type mismtach." (Shackelford)
  • The Rationale:
    • "Disciplined thinking"
Type Matching Reconsidered
Back SMYC Forward
  • Recall:
    • All numbers are represented in binary
    • Different types use different number of bits
  • Implication:
    • Type mismtaches aren't always a problem
Unsigned Integers
Back SMYC Forward
  • Types:
    • byte (1 byte)
    • short (2 bytes)
    • int (4 bytes)
    • long (8 bytes)
  • Conversions:
    • byte to short: pad with one byte of 0s
    • short to int: pad with two bytes of 0s
    • int to long: pad with four bytes of 0s
  • These are called widenings
Unsigned Integers (cont.)
Back SMYC Forward
  • An example byte (19 base 10):
    • 00010011
  • Conversion to short:
    • 00000000 00010011
Going Further: Twos Complement
Back SMYC Forward
  • An example byte (-19 base 10):
    • 11101101
  • Conversion to short:
    • 11111111 11101101
Going Further: Twos Complement (cont.)
Back SMYC Forward
  • -128 base 10 (byte):
    • 10000000
  • 128 base 10 (short):
    • 00000000 10000000
  • -128 base 10 (short):
    • 11111111 10000000
Floating Point Numbers
Back SMYC Forward
  • float:
    • 1 bit for the sign
    • 8 bits for the exponent
    • 23 bits for the mantissa
  • double:
    • 1 bit for the sign
    • 11 bits for the exponent
    • 52 bits for the mantissa
Floating Point Numbers (cont.)
Back SMYC Forward
  • float to double Conversions:
    • Pad the exponent and mantissa with 0s
  • long to float Conversions:
    • The largest long is 9,223,372,036,854,775,807
    • This becomes the float 9,223,372,036,854,776,000
    • This is a loss of precision but not magnitude
    • The IEEE 754 round-to-nearest rule is applied
Implicit Widenings in Java
Back SMYC Forward
int small; long large; . . . large = small;
Implicit Numeric Promotion in Java
Back SMYC Forward
  • Definition:
    • Widening that occurs within the context of a numeric operator
  • Why is this an Issue?
    • The conversion chosen for one operand may depend in part on the type of the other operand expression
  • Example:
    • large = large + small;
Explicit Narrowings in Java
Back SMYC Forward
  • Typecasting:
    • (Type)Expression
  • Examples:
      int small; long large; small = (int)large;
Narrowing Conversions (cont.)
Back SMYC Forward
  • Integer to Integer:
    • Discard the lowest order bits
  • Floating Point to Integer:
    • Involves a complicated process
Narrowing Conversions (cont.)
Back SMYC Forward
  • Result:
    • Loss of precision
    • Possible loss of magnitude
    • Possible change of sign
  • Implication:
    • Use sparingly, if at all
Narrowing Conversions (cont.)
Back SMYC Forward
public class NarrowingExample { public static void main(String[] args) { float fmax, fmin; fmax = Float.POSITIVE_INFINITY; fmin = Float.NEGATIVE_INFINITY; JMUConsole.println("long: "+(long)fmin +".."+(long)fmax); JMUConsole.println("int: "+(int)fmin +".."+(int)fmax); JMUConsole.println("short: "+(short)fmin+".."+(short)fmax); JMUConsole.println("byte: "+(byte)fmin +".."+(byte)fmax); } }
There's Always More to Learn
Back -