1 / 88

Building Abstractions with Data (Part 3)

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.

liluye
Download Presentation

Building Abstractions with Data (Part 3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building Abstractions with Data (Part 3) CS 21a: Introduction to Computing I First Semester, 2013-2014

  2. Outline • Assignment and State • Pass by Value versus Pass by Sharing • Message Passing and Final Remarks on OOP

  3. Two Different Kinds of Systems Without State With State

  4. (Re)Assignment Produces State Without State With State intmoney = 500; money = deposit(money, 1000); intmoney = 500; intmoneyAfterDeposit = deposit(money, 1000);

  5. (Re)Assignment Produces State Without State With State intmoney = 500; money = money+ 1000; intmoney = 500; intmoneyAfterDeposit = money + 1000;

  6. (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 (==).

  7. 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 //

  8. (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

  9. (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

  10. (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

  11. 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

  12. Changing Object State By Updating Fields public class Diet { intcalories; public Diet() { calories = 0; } publicvoidrecordBurgerEaten() { calories += 400; } }

  13. Changing Object State By Updating Fields public class Diet { intcalories; public Diet() { calories = 0; } publicvoidrecordBurgerEaten() { calories += 400; } } Mutator method

  14. 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

  15. 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

  16. Outline • Assignment and State • Pass by Value versus Pass by Sharing • Message Passing and Final Remarks on OOP

  17. 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

  18. 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

  19. intx = 1000; Primitive Type Variablesand Assignment x 1000

  20. intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000

  21. intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000 Pass by value: y gets a copy of the value of x

  22. intx = 1000; inty = x; Primitive Type Variablesand Assignment x 1000 y 1000 Pass by value: y gets a copy of 1000

  23. intx = 1000; inty = x; y = y - 100; println( x ); println( y ); Primitive Type Variablesand Assignment x 1000 y 900 Prints: 1000 900

  24. Object Variablesand Assignment b balance 1000 BankAccount b = newBankAccount( 1000 );

  25. Object Variablesand Assignment b balance 1000 c BankAccount b = newBankAccount( 1000 ); BankAccount c = b;

  26. 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

  27. 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.

  28. 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

  29. 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);

  30. 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!

  31. 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

  32. 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

  33. What’s a Pointer? • So when you do… c.withdraw(100); • The balance is changed for the instance that both b and c share

  34. 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.

  35. 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

  36. 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 );

  37. The nullPointer • An object variable that doesn’t point to an actual instance yet has a nullpointer (points to nothing)

  38. 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)

  39. An Analogy to Clear That Up

  40. An Analogy to Clear That Up

  41. An Analogy to Clear That Up balance newBankAccount(500); 500 some memory address

  42. An Analogy to Clear That Up balance BankAccount b = newBankAccount(500); 500 b

  43. 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

  44. 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

  45. 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

  46. 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

  47. An Analogy to Clear That Up balance BankAccount c = b; 1000 b c

  48. An Analogy to Clear That Up balance c.deposit(500); 1500 b c

  49. An Analogy to Clear That Up balance c = null; // c.deposit(500); -> NullPointerException 1500 b c

  50. An Analogy to Clear That Up balance BankAccount d; // d.deposit(500); -> compile error 1500 b c d

More Related