name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Operations on Variables ] ??? * **P** speaker view * **C** clone slideshow * **?** help --- # Java Operators * On numeric types, the following operators work mostly as expected: + `+` — addition + `-` — subtraction + `*` — multiplication + `/` — division -- * Each program has one `main()` method that specifies instructions ``` Java System.out.println(3 + 5); // 8 System.out.println(3 * 5); // 15 System.out.println(3.2 - 5.4); // -2.2 System.out.println(5.0 / 2.0); // 2.5 ``` -- * Some operations may not be as expected — integer division ``` Java System.out.println(5 / 2); // 2 (ignores anythig after the decimal place) ``` --- # Operator Precedence * Order of operations is similar to what you would expect from algebra -- * See Figure A2.1 in Appendix II of textbook -- * Can use parentheses to force order of operations ``` Java System.out.println(3 + 5 * 2); // 3 + 10 = 13 System.out.println((3 + 5) * 2); // 8 * 2 = 16 System.out.println(1 + 2 * 3 - 4); // 1 + 6 - 4 = 7 - 4 = 3 System.out.println(((1 + 2) * 3) - 4); // (3 * 3) - 4 = 9 - 4 = 5 ``` --- # Binary versus Unary Operations * Unary operations operate on one operand, e.g., `-a` -- * Binary operations operate on two operands, e.g., `a + b` -- * Both operands must have the same type -- * If operand types differ, either + Operation fails, or + One operand is automatically converted to matching type prior to operation --- # Automatic Type Conversions * Integers are automatically converted to floating-point types, `int` -> `double` -- * Smaller types are automatically converted to larger types, `int` -> `long` -- * Examples ``` Java System.out.println(3 / 2); // 1 (truncated integer division) System.out.println(3 / 2.0); // 1.5 System.out.println(33333333333L * 2); // 66,666,666,666 ``` --- # Non-automatic Type Conversions * Java will not automatically convert floating-point to integer -- * Java will not automatically convert larger type to smaller type -- * **Type casting** — technique for forcing such a conversion -- * Place the desired type in parentheses before the variable or literal to be converted -- * Examples ``` Java int i = 3; double x = 1.5; System.out.println((double)i); // 3.0 System.out.println(i / (int)x); // 4 i = (int)x * 2; // 1 + 2 = 2 i = (int)(x * 2); // (double)(3.0) = 3 ``` --- # Additional Operators * `%` — modulus operator -- * `a % b` results in the remainder of `a` divided by `b` -- * `a *= b` is short for `a = a * b` -- * Other compound assignment operators: `/=`, `+=`, `-=`, and `%=` --- # Operations on Strings * The `String` class is special in several ways -- * The `+` operator can be used with `String`s -- * Binary operator that concatenates two `String`s together -- * Examples ``` Java String first = "Jon"; String last = "Dough"; String fullname = first + " " + last; // "Jon Dough" ``` -- * `+` operations on `String`s also invokes automatic type conversion of primitives if left operand is a `String` --- # Operations on Strings * Examples ``` Java String first = "Jon"; String last = "Dough"; String fullname = first + ' ' + last; // "Jon Dough" first = first + 8; // "Jon8" last = last + 1.11; // "Dough1.11" last = 1.11 + last; // "1.11Dough" first = 2 + 2 + last; // "4Dough" first = last + 2 + 2; // "Dough22" ``` --- # Relational Operators * Relational operators evaluate to a boolean value -- * Most are what you'd expect: `<`, `<=`, `>=`, and `>` -- * Equality: `==` -- * Inequality: `!=` -- * Unary **not** operator: `!` --- # Wrapper Classes * Converting from `String` to primitive can be done with **wrapper classes** -- * Wrapper class provides methods to convert to primitive + `int i = Integer.parseInt("14");` + `double x = Double.parseDouble("14.7");` --- # Input and Output Stream * Interaction through the terminal window happens via input and output streams -- * `System.in` — input stream for getting data into our program -- * `Scanner` — Class used to consume data from the input stream + Converts bytes from input stream to `int`s, `double`s, `String`s, etc... -- * `System.out` — output stream for displaying data in the terminal window + Use `System.out.println(data)` to output data to the console