COSI 11a Chapter Notes - Chapter 2: Concatenation, Increment And Decrement Operators
Chapter 2 Outline
Primitive Data and Definite Loops
Pages 61 – 131
I. Basic Data Concepts
A. Arithmetic Operators
1. In integer division, everything after the decimal is truncated. For example, 1079/34 = 31. The
remainder (mod) operator accounts for the truncated part. For example, 1079%34 = 25.
2. By putting the name of a type in parenthesis in front of a value, the value can be cast into that
type. For example, the double 5.61 can be cast into the integer 5 by using (int) 5.61.
II. Variables
A. Assigning and Declaring Variables
1. Unlike most operators, the assignment operator evaluates from right to left. For example,
x = y = z = 2 * 5 + 4 is evaluated as x = (y = (z = 2 * 5 + 4)).
2. Addition operators are evaluated like most operators, from left to right, so the string
concatenation: 5 + “ hello ” + 7 + 6 becomes “5 hello ” + 7 + 6, then “5 hello 7”, and finally “5
hello 76”. For example, (2 + 3) + “hello” + (7 + 2 * 3) is “5hello13”.
B. Increment and Decrement Operators
1. ++x is the preincrement and x++ the postincrement. The postincrement is read as “give me x,
then increment”, while the preincrement is “increment, then give x”.
III. Managing Complexity
A. Class Constants
1. The value of a class constant cannot be changed. They can be accessed throughout the entire
class. The syntax of a class constant is: public static final <type> <name> = <expression>;.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Pages 61 131: basic data concepts, arithmetic operators, in integer division, everything after the decimal is truncated. The remainder (mod) operator accounts for the truncated part. For example, 1079%34 = 25: by putting the name of a type in parenthesis in front of a value, the value can be cast into that type. For example, the double 5. 61 can be cast into the integer 5 by using (int) 5. 61. Variables: assigning and declaring variables, unlike most operators, the assignment operator evaluates from right to left. For example, (2 + 3) + hello + (7 + 2 * 3) is 5hello13 : increment and decrement operators, ++x is the preincrement and x++ the postincrement. The postincrement is read as give me x, then increment , while the preincrement is increment, then give x . Managing complexity: class constants, the value of a class constant cannot be changed. They can be accessed throughout the entire class.