next up previous
Next: Week 5 Up: Week 4 Previous: What's the deal with

Conversions

Narrowing, Widening, when do conversions occur, what happens.

  1. Widening Conversions: no loss of 'information'
  2. Examples:
    1. char to int or double
    2. int to double
  3. Narrowing Conversions: potential loss of 'information'
  4. Examples:
    1. double to char or int
  5. booleans undergo no conversions

  6. Primitive DATA TYPES: When does conversion occur?
    1. Assignment Conversion: Widening automatic
    2. Method Invocation Conversion: Widening automatic
    3. Casting Conversion: can widen/narrow with explicit statement
    4. Numeric Promotions in expressions (=-/*): If either operand is of type double, the other is converted to double. Otherwise, both operands are converted to type int.

      char int double boolean
    char auto auto auto none
    int cast auto auto none
    double cast cast auto none
    boolean none none none auto

  7. STRINGS: String Conversion (+ operation when one operand is string) takes any type to string. To go from a String to a numeric type, use Integer.parseInt() to int, Double.parseDouble() to double, etc.


next up previous
Next: Week 5 Up: Week 4 Previous: What's the deal with
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09