880 likes | 988 Views
Building Abstractions with Data (Part 3). CS 21a: Introduction to Computing I First Semester, 2013-2014. Outline. Assignment and State Pass by Value versus Pass by Sharing Message Passing and Final Remarks on OOP. Two Different Kinds of Systems. Without State. With State.
E N D
Building Abstractions with Data (Part 3) CS 21a: Introduction to Computing I First Semester, 2013-2014
Outline • Assignment and State • Pass by Value versus Pass by Sharing • Message Passing and Final Remarks on OOP
Two Different Kinds of Systems Without State With State
(Re)Assignment Produces State Without State With State intmoney = 500; money = deposit(money, 1000); intmoney = 500; intmoneyAfterDeposit = deposit(money, 1000);
(Re)Assignment Produces State Without State With State intmoney = 500; money = money+ 1000; intmoney = 500; intmoneyAfterDeposit = money + 1000;
(Re)Assignment Produces State Without State With State intmoney = 500; money = money+ 1000; intmoney = 500; intmoneyAfterDeposit = money + 1000; Assignment operator (=) not to be confused with mathematical equals (==).
By The Way, Shortcuts! money += 1000;// money = money + 1000; money++; // money += 1; money -= 500; // there are also *= and /= money--; // doesn’t make sense to have ** and //
(Re)Assignment Produces State Without State With State Same name for both “input” and “output” Changing an existing thing • Input and output values have different names • Make a new thing out of an existing thing
(Re)Assignment Produces State Without State With State Variables are boxes whose contents can be changed Makes sense if what a name refers to is a changing, dynamic entity • Variables are names attached to values • Doesn’t make sense to change what something means
(Re)Assignment Produces State Without State With State Operations can produce side effects (aside from producing output, also changes state) Result of operation is dependent on time, history, environment intx = 5; inty = ++x; // now 6 intz = ++x; // now 7 • Applying the same operations to the same variables always produces the same results. • Operation can be understood by plain substitution intx = 5; inty = x + 1; // 6 intz = x + 1; // still 6
Pre-increment versusPost-increment • See example here • Difference in evaluation… • Is used a lot in older programs • Is useful for some accepted idioms • Should not be abused to make ambiguous programs • Use these expressions often for side effects, sparingly for the value
Changing Object State By Updating Fields public class Diet { intcalories; public Diet() { calories = 0; } publicvoidrecordBurgerEaten() { calories += 400; } }
Changing Object State By Updating Fields public class Diet { intcalories; public Diet() { calories = 0; } publicvoidrecordBurgerEaten() { calories += 400; } } Mutator method
Changing Object State By Updating Fields Diet myDiet = new Diet(); // myDiet has 0 calories myDiet.recordBurgerEaten(); println(myDiet.calories); // myDiet has 400 calories myDiet.recordBurgerEaten(); println(myDiet.calories); // myDiet has 800 calories
When to Use Mutators? • Only where it makes sense (updating bank account, calorie count, etc.) • Example where it does not make sense: Fraction’s add
Outline • Assignment and State • Pass by Value versus Pass by Sharing • Message Passing and Final Remarks on OOP
Two Kinds of Types in Java • Primitives types • int, double, long, short, byte, float, char, boolean • A variable holds a valid value • Object types • Java built-ins: String, Scanner • User-defined: Fraction, BankAccount • A variable holds a pointer to an object
Primitive Type Variablesversus Object Variables b balance 0 • Primitive type variables directly contain values intx = 5; • Object variables contain pointers to instances BankAccount b = newBankAccount(); x 5
intx = 1000; Primitive Type Variablesand Assignment x 1000
intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000
intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000 Pass by value: y gets a copy of the value of x
intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000 Pass by value: y gets a copy of 1000
intx = 1000; inty = x; y = y - 100; println( x ); println( y ); Primitive Type Variablesand Assignment x 1000 y 900 Prints: 1000 900
Object Variablesand Assignment b balance 1000 BankAccount b = newBankAccount( 1000 );
Object Variablesand Assignment b balance 1000 c BankAccount b = newBankAccount( 1000 ); BankAccount c = b;
Object Variablesand Assignment b balance 1000 c BankAccount b = newBankAccount( 1000 ); BankAccount c = b; Pass by sharing: c gets a copy of the value of b
Object Variablesand Assignment b balance 1000 c BankAccount b = newBankAccount( 1000 ); BankAccount c = b; Pass by sharing: c gets a copy of a pointer to the bank account. NO NEW OBJECT INSTANCE IS CREATED.
Object Variablesand Assignment b balance 900 c BankAccount b = newBankAccount( 1000 ); BankAccount c = b; c.withdraw( 100 ); println( b.getBalance() ); println( c.getBalance() ); Prints: 900 900
What’s a Pointer? • It’s really a memory address! • Try this: BankAccount b = newBankAccount( 1000); println(b); BankAccount c = newBankAccount( 1000 ); println(c); println(c == b); c = b; println(c); println(c == b);
What’s a Pointer? • It’s really a memory address! • Try this: BankAccount b = newBankAccount( 1000); println(b); BankAccount c = newBankAccount( 1000 ); println(c); println(c == b); c = b; println(c); println(c == b); Doesn’t matter if the two bank accounts have the same balance. They’re different accounts!
What’s a Pointer? • It’s really a memory address! • Try this: BankAccount b = newBankAccount( 1000); println(b); BankAccount c = newBankAccount( 1000 ); println(c); println(c == b); c = b; println(c); println(c == b); Copies the memory address stored in b to c
What’s a Pointer? • It’s really a memory address! • Try this: BankAccount b = newBankAccount( 1000); println(b); BankAccount c = newBankAccount( 1000 ); println(c); println(c == b); c = b; println(c); println(c == b); c now points to the same object as b does
What’s a Pointer? • So when you do… c.withdraw(100); • The balance is changed for the instance that both b and c share
What Happens to the Second Bank Account? • Garbage collection is a feature of a programming language, where objects that can no longer be accessed are automatically destroyed.
Method Calls • A method is invoked on an object variable but acts on the object that the variable is pointing to b BankAccount b = newBankAccount(1000); balance 1000
Method Calls b balance deposit(250) 1250 • A method is invoked on an object variable but acts on the object that the variable is pointing to BankAccount b = newBankAccount(1000); b.deposit( 250 );
The nullPointer • An object variable that doesn’t point to an actual instance yet has a nullpointer (points to nothing)
Method Calls on null • Calling a method on an object variable whose value is null results in an error (NullPointerException) b null BankAccountb = null; b.deposit( 250 ); ??? deposit(250)
An Analogy to Clear That Up balance newBankAccount(500); 500 some memory address
An Analogy to Clear That Up balance BankAccount b = newBankAccount(500); 500 b
An Analogy to Clear That Up balance b.deposit(500); Three parts: What’s b? What’s the pointer pointing to? Apply operation to object 500 b
An Analogy to Clear That Up balance b.deposit(500); Three parts: What’s b? What’s the pointer pointing to? Apply operation to object 500 b
An Analogy to Clear That Up balance b.deposit(500); Three parts: What’s b? What’s the pointer pointing to? Apply operation to object 500 b
An Analogy to Clear That Up balance b.deposit(500); Three parts: What’s b? What’s the pointer pointing to? Apply operation to object 1000 b
An Analogy to Clear That Up balance BankAccount c = b; 1000 b c
An Analogy to Clear That Up balance c.deposit(500); 1500 b c
An Analogy to Clear That Up balance c = null; // c.deposit(500); -> NullPointerException 1500 b c
An Analogy to Clear That Up balance BankAccount d; // d.deposit(500); -> compile error 1500 b c d