BOOK NOTES CHAPTER 4 4.1 NUMBER TYPES - every value is either an object reference or a primitive - p. 105 table of primitive types, usages, and range of values - 8 types - 6 numeric - 4 integer - 2 floating point - generally use int for integers - overflow: result of computation exceeds range for number type - value truncated to fit - result is wrong - no warning - long - correct int overflow - if overflows, use BigInteger - floating point - use double b/c float has low precision - rounding error - can't always convert exactly - binary number system can't represent decimal fractions exactly - must use BigDecimal for real financial applications - casting - can assign integers to floating point values - cannot assign floating point values to integers - explicitly convert between types - only required from more precise to less precise - synatx: () expression - tell compiler that agree to information loss - (int) truncates floating point part - use Math.round to round numbers - static long round(double x) - static int round(float x) 4.2 CONSTANTS - numerical constants - values that do not change - have special significance - use symbolic names for all values - declare a constant - final modifier - if try to change, get compile-time error - naming convention - all capital letters - use underscores to separate words - need constant in several methods - declare with instance data - also declare static - declare public because cannot be modified - examples: double Math.PI, double Math.E - don't use numeric constants in your code w/out explanation - choose descriptive variable names: important for sharing code 4.3 ASSIGNMENT, INCREMENT, AND DECREMENT - increment operator - ++ - increases the value of its operand by one - decrement operator - -- - decreases the value of its operand by one - combining assignment and arithmetic - a op= b - equivalent to a = a op b 4.4 ARITHMETIC OPERATIONS AND MATHEMATICAL FUNCTIONS - division operator: / - parenthetical expressions are evaluated first - multiplication and division bind more strongly than addition and subtraction - division - works as expected if at least one operand is floating point - when both operands are integers, the result is a truncated integer - modulo: remainder of an integer division - x^n - Math class - static double pow(double x, doublen) - for squares, more efficient to write out - square root - Math class - static double sqrt(double x) - p. 120 Math methods - whitespace - put spaces around all binary operators - put a space after every Java keyword 4.5 CALLING STATIC METHODS - static method - doesn't operate on an object - is defined in a class - specify with name of class - naming convention useful in identifying - object names always start with a lowercase letter - class names always start with a capital letter 4.6 STRINGS - string: sequence of characters - enclosing quotation marks - used by Java to specify - not part of string - object - length - number of characters in it - String int length() - empty string - no characters - written "" - concatenation - put strings together to form a longer string - + operator - provided at least one argument is a string - forces other argument to type string - reduce number of System.out.print instructions - convert a string to an int: Integer static int parseInt(String x) - convert a string to a float: Double static double parseDouble(String x) - String indices start at 0 - String methods - String substring(int startPosition, int pastEnd) - returns the String - from index startPosition - up to but not including index pastEnd - String substring(int startPosition) - including startPosition through the end of the implicit parameter - unreasonable parameters cause program to terminate with error message 4.7 READING INPUT - use System.in object - use java.util.Scanner class - pass System.in to the Scanner constructor - int nextInt() method lets you read the next int - double nextDouble() method lets you read the next double - always prompt user before attempting to get input - input is sent to your program when user hits Enter key - String nextLine() gets all of the input as a String - String next() gets the next word (terminated by white space)