Data Assignment Rules ◆ Compatibility of Data Assignments ◆Type mismatches General Rule:Cannot place value of one type into variable of another type ◆intVar=2.99;∥2 is assigned to intVar! Only integer part "fits",so that's all that goes Called "implicit"or"automatic type conversion" ◆Literals ◆2,5.75,"Z","Hello World" Considered"constants":can't change in program Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-11 Data Assignment Rules Compatibility of Data Assignments Type mismatches General Rule: Cannot place value of one type into variable of another type intVar = 2.99; // 2 is assigned to intVar! Only integer part "fits", so that’s all that goes Called "implicit" or "automatic type conversion" Literals 2, 5.75, "Z", "Hello World" Considered "constants": can’t change in program
Literal Data ◆Literals ◆Examples: ◆2 /Literal constant int ◆5.75 /Literal constant double ◆"Z" /Literal constant char ◆"Hello World" /Literal constant string Cannot change values during execution Called "literals"because you "literally typed" them in your program! Copyright006 Pearson Addison-Wesley.All rights reserved. 1-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-12 Literal Data Literals Examples: 2 // Literal constant int 5.75 // Literal constant double "Z" // Literal constant char "Hello World" // Literal constant string Cannot change values during execution Called "literals" because you "literally typed" them in your program!
Escape Sequences "Extend"character set Backslash,preceding a character Instructs compiler:a special "escape character"is coming Following character treated as "escape sequence char" Display 1.3 next slide Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-13 Escape Sequences "Extend" character set Backslash, \ preceding a character Instructs compiler: a special "escape character" is coming Following character treated as "escape sequence char" Display 1.3 next slide
Display 1.3 Some Escape Sequences(1 of 2) Display 1.3 Some Escape Sequences SEQUENCE MEANING An New line Carriage return(Positions the cursor at the start of the current line.You are not likely to use this very much.) \t (Horizontal)Tab (Advances the cursor to the next tab stop.) Alert(Sounds the alert noise,typically a bell.) Backslash(Allows you to place a backslash in a quoted expression.) Copyright006 Pearson Addison-Wesley.All rights reserved. 1-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-14 Display 1.3 Some Escape Sequences (1 of 2)