Copyright © 2005-2007 William C. BentonChapter 41Copyright © 2005-2007 William C. BentonFundamental types•Remember: Java has two kinds of types•primitive types•e.g. int, float, etc.•reference types•all object and array types•What are the differences?2Copyright © 2005-2007 William C. BentonPrimitive types•Three basic categories:•Whole numbers•Numbers with fractional parts•Truth values (i.e. boolean)3Copyright © 2005-2007 William C. BentonWhole-number types4namesizerangelong8 bytes+/- 9.2 * 1018int4 bytes+/- ~2 billionshort2 bytes-32768 - 32767byte1 byte-128 - 127char2 bytesUnicodeCopyright © 2005-2007 William C. BentonFractional-number types5namesizerangedouble8 bytes+/- 10308 15 sig. float4 bytes+/- 1038 7 sig. digitsCopyright © 2005-2007 William C. BentonArithmetic operators6z = -x;unary z = +x;unary positivez = x % y;modulusz = x / y;divisionz = x * y;multiplicationz = x - y;subtractionz = x + y;addition123456Copyright © 2005-2007 William C. BentonNumber basics•We’ve seen string literals; there are also numeric literals:•1234L (long)•1234.0F (float)•1234.0D (double)•Why is it important to be able to declare the type of a numeric literal?7Copyright © 2005-2007 William C. BentonArithmetic operations•Add, subtract, and multiply work as you’d expect•Two kinds of division•Integer division•Floating-point division•Modulus/remainder8Copyright © 2005-2007 William C. BentonInteger vs. FP division•Floating-point division works as you’d expect:•5.0F / 2.0F == 2.5F•Integer division discards the remainder:•5 / 2 == 2•Which is used for given operands?•Integer if all operands are integers•FP if any operand is floating-point9Copyright © 2005-2007 William C. BentonAssignment•Remember the assignment operator?•x = 5; // “x gets the value 5”•Other assignment operators:•x++; // “increment x, evaluate to old x”•++x; // “increment x, evaluate to new x”•x+=5; // “increment x by 5”•Also: *=, -=, /=10Copyright © 2005-2007 William C. BentonOperator precedence•This slide is simple:when in doubt, use parentheses!11Copyright © 2005-2007 William C. BentonThings to lose sleep over•Overflow•Integer types have limited range•Rounding errors•Division by zero•Unfortunately, still undefined. May crash your program.12789101112Copyright © 2005-2007 William C. BentonOverflow13Copyright © 2005-2007 William C. BentonRounding errors•Floating-point arithmetic is not totally precise.•Remember, FP types only have a finite number of significant digits•Good enough for most applications, maybe not for all•Rounding errors can be magnified in a sequence of operations•Financial institutions, etc., use (slow but) precise classes for FP math14Copyright © 2005-2007 William C. BentonCasting•You can treat an expression as if it has a different type:•(typeName)exp•Example: (int)4.2F•Why do this? What are the tradeoffs?15Copyright © 2005-2007 William C. BentonString16•Strings are very useful•Many of the methods in String will make your life easier•Note that none of these modifies the base String -- String is immutable.•You’ll cover these more in lab 5•You can use the + operator to concatenate StringsCopyright © 2005-2007 William C. BentonString s1 = “x”;String s2 = s1 + s1;String s3 = “banana”;String s4 = s3.substring(1, 3);String s5 = s4.replace(‘a’, ‘o’);17Copyright © 2005-2007 William C. BentonWrapper classes•What’s a primitive type?•What’s a reference type?•Remember that Java maintains a divide between primitive types and reference types. Wrapper classes provide a way around this!18131415161718Copyright © 2005-2007 William C. BentonWrapper classes•One for each primitive type: e.g. Integer for int, etc.•Can make an Integer from an int, and can get the int value from an Integer•All wrapper classes are immutable, just like String.•Why might we use these?19Copyright © 2005-2007 William C. BentonConstants: why?•What does the following line of code mean?•x = 42 * y;•What does “42” mean? Why?•Constants make programs easier to read and maintain.20Copyright © 2005-2007 William C. Bentonfinal locals•final int NUM_SHELVES = 42;•x = NUM_SHELVES * y;• Can only be assigned to once!21Copyright © 2005-2007 William C. BentonConstants22•Why are we interested in constants?•How can we use named constants in our Java programs?Copyright © 2005-2007 William C. BentonConstants: Why?•Even if we choose good names for local variables, “magic numbers” can make our programs worse•harder to understand•harder to maintain•Consider:•products = 42 * perShelf; // vs.•products = NUM_SHELVES * perShelf;23Copyright © 2005-2007 William C. BentonConstants: How?24public int totalProducts(int pps) { final int NUM_SHELVES = 42; return NUM_SHELVES * pps;}192021222324Copyright © 2005-2007 William C. BentonConstants: How?25public int totalProducts(int pps) { final int NUM_SHELVES = 42; NUM_SHELVES = 36; return NUM_SHELVES * pps;}Copyright © 2005-2007 William C. BentonConstants: How?25public int totalProducts(int pps) { final int NUM_SHELVES = 42; NUM_SHELVES = 36; return NUM_SHELVES * pps;}Copyright © 2005-2007 William C. BentonConstants: How?25public int totalProducts(int pps) { final int NUM_SHELVES = 42; NUM_SHELVES = 36; return NUM_SHELVES * pps;}final variablescan only getone value!Copyright © 2005-2007 William C. BentonA limitation of final locals•What if you want to use the same constant in multiple methods?•Is there a good way to do this?•Is there any way to do this at all?26Copyright © 2005-2007 William C. BentonWell....•You could just declare a final local in each method that is to use the constant.•That’s sort of clunky.•Duplicating constant declarations in each method is tedious and error-prone.•Why are we using constants in the first place?27Copyright © 2005-2007 William C. BentonIf some programming task is tedious and error-prone, it probably indicates bad design, bad style, or both!28252525262728Copyright © 2005-2007 William C. BentonA better solution29•final data members•Two kinds:•final instance variables•static final variables•Different applications for eachCopyright © 2005-2007 William C. Bentonfinal instance vars•These correspond to something that can’t change once the object is created•“Factory-installed options”•Examples:•“Parents”
View Full Document