******************************************************************************* 0) Last time- quick tour of data, methods, objects - quick quiz for chapters 0-2 1) Administrative - 2nd codelab due date tomorrow, as you know, and A0 is Friday (questions?) - hw: read 3.4-3.7, next person presents 2) Split in groups and go over 3.1-3.3 3) Highlights of material A) Numerical types: how precise, and how big a range * book error- what does precision mean? -sig figs, not range * two basic precision groups: whole numbers, floating point * don't memorize, but have a basic idea to avoid problems Whole number types: all have integer precision * byte (~120) short (~32000) int** (~2 billion) long (~10^18) Floating point types: decimals, or scientific notation * float (~10^38, ~9 places of precision) * double** (~10^308, ~18 places of precision) B) Literal constants have types, too (and of course named constants do) * Whole number? -int Make it long? -put an L after * Floating point? -double Make it float? -put an F after Explicitly double? -put a D after * How to write scientific notation? (12.40e209) * How to make negative (or explicitly positive)? -put - or + D) Arithmetic operators? -operate on one or two expressions (operands) * unary/binary? (1,2) examples? ( -,+ and -,+,*,/,%) * what's %? -modulo operator; remainder (do 4 % 1,2,3,4,5) * what's tricky about /? -integer division (10/4, 5/4, 4/5) * note that same operator can have 2 different meanings * actually, + three; what's third? -string concatenation Arithmetic expression? -anything that evaluates to a number * 5 1+7 x / (y-2) f(1)%3 2*PI*r (subexpressions) Assignment: what's wrong with this? x + 1 = y * can't put an expression on the left side; only a variable E) How do you tell order of operation? -precedence rules (familiar) * things in parens always come before things outside * unary + and - are next * then *, /, and % (left to right) * then binary + and - (left to right) F) Type conversions between numeric types (not others!) * types can be ordered from "narrow" to "wide": - byte -> short -> int -> long -> float -> double - conversions- widening is ok, narrowing loses information - often widening is automatic, but narrowing is an error * assignment conversion: double d = 5; (ok) int i = 5.0; (bad) * numeric promotion: done automatically to make operands ok - bytes or shorts always become ints (they're too little) - then, if one operator is wider, other is widened to match - 5.0/2 -> 2.5 * casting: done when you want to explicitly force a conversion - put new type in parens just before value - (int) 5.0 -> 5 , (float) 5 / 2 -> 2.5 , (float)(5/2) -> 2.0 - this is the only legal way to narrow 4) Exercises A) Evaluate the following: 5 - 3 * 1 + 4 / 2 (precedence, left to right) -> 4 1 / 2 * 100 - 1 (integer division, left to right) -> -1 10 * (7 % 4) - (2 + 3) (parentheses, mod operator) -> 25 (1 + 1 + -1) / 4.0F (promotion, negatives, literals) -> 0.25 (int)6.0 + 1 / 3e0 (casting, scientific notation) -> 6.333... (double)(3 * 3) / -2L (casting, negatives, literals) -> -4.5 B) Store the average of three ints (x,y,z) in the double d. d = (x + y + z) / 3.0; (and several other ways) C) Which of these are legal? int x = 3L; int y = (int)5e-200; *byte b = (byte)1.2; *double d = 3L; short s = 1+1; long z = (long) 4 / 2.0; D) What are memory diagrams like after these lines? (aliasing) int x = 3; JFrame x = new JFrame(); x.setSize(10,10); int y = x; JFrame y = x; y.setSize(20,20); 5) Summary - what the numerical types are - how to use operators and evaluate expressions - how conversions between types happen - how aliasing happens in reference types *******************************************************************************