CS151 lecture notes, Fall 1999 Week 2, Wednesday Reading: Chapter 2 Quiz 2 Operators and operands ---------------------- + - * All do what you expect. Integer division is not what you expect! int minute; minute = 59; System.out.println (minute / 60); Always rounds to the lower integer (truncation). There's another type for dealing with fraction parts. You can use values AND variables as operands. Values, variables, and operators together are called "expressions". Order of operations ------------------- PEMDAS, as usual (expect there is no exponentiation argument). Within each category, left to right. 2*3-1 = 5 2/3-1 = -1 Operations on Strings --------------------- + is String concatenation. String fred = "leather jacket"; String bigotry = fred + "ism"; System.out.println (bigotry); All expressions have types -------------------------- Most cases, the operands have to have the same type and the result has the same type again: int + int -> int String + String -> String What happens if you try to combine a String and an integer? String fred; int bob; System.out.println (fred + bob); Strictly, that's just not legal. Often convenience outweighs strict type theory. In Java, String + int -> String Which is accomplished by converting the int to a String and performing concatenation. Is that arbitrary? Would it be just as good to convert the String to an int and add? Is one or the other theoretically preferable? Composition ----------- The ability to assemble building-blocks into larger structures, often by nesting one structure within another. Example: expressions made up of smaller expressions Almost an example: anywhere you can put a simple value, you can also put a complex expression. Exception: the left side of an assignment must be a variable, since it specifies where the result will be stored. Values and expressions do not have locations. minute+1 = hour; // WRONG!! 3x5 vocab: variable, value, type, keyword, statement, declaration, assignment, expression, operator, operand, precedence, concatenate, composition START READING CHAPTER 3 FOR FRIDAY