Next: Week 5
Up: Week 4
Previous: What's the deal with
Narrowing, Widening, when do conversions occur, what happens.
- Widening Conversions: no loss of 'information'
- Examples:
- char to int or double
- int to double
- Narrowing Conversions: potential loss of 'information'
- Examples:
- double to char or int
- booleans undergo no conversions
- Primitive DATA TYPES: When does conversion occur?
- Assignment Conversion: Widening automatic
- Method Invocation Conversion: Widening automatic
- Casting Conversion: can widen/narrow with explicit statement
- 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 |
- 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: Week 5
Up: Week 4
Previous: What's the deal with
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.
2001-12-09