Lecture 6 - Data Conversions Widening (automatic)...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Data Conversions Widening (automatic) conversions o Are safe because you go from a small data unit to a larger one (not losing any information) o Eg: short to an int or float float to a double Narrowing conversions o Are unsafe because information may be lost since you go from a large unit to a smaller one o Eg: int to byte double to float o Must be forced with a cast Casting o General form: (target-type)value o Eg: int dollars; float money = 3.41F; dollars = (int)money; // truncated to 3 NOTE: money is only 3 in that line of code, not changed Data conversions can happen in 4 ways: o (1)Casting o (2)Arithmetic promotion Widening conversions are done automatically in mixed type expressions Eg: float result, factor; int i; ... result = i * factor; // i is promoted to float o (3)Assignment Done automatically if safe to do so
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Eg: double x; int y = 27; x = y; // 27 is converted to 27.0; o (4)Passing parameters Done automatically if safe to do so Eg: int x;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Lecture 6 - Data Conversions Widening (automatic)...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online