CS 280 Data Structures Professor John Peterson
Exam 1 Stuff O(Log(n)) + O(n) = ? for (int i = n; i>0; i=i/2) for (int j = 0; j < i; j++) print(j); n*log(n) n = 100, t = 10, n = 1000, t = ? 15: Let’s do this now! Know it!!
This Week No project other that #7 from last week Homework is a very short program due Monday since there’s no class Friday Exam will be Monday
Types of Objects Primitive Compare values with == Never need to say “new” (no constructor) Fixed vocabulary Passed by value A “boxed type” is available – implicit coercion x = y copies value No internal structure Default depends on type Ordinary == compares identity, not values Always need to say “new” to make a value Can add user-defined classes Passed by reference x = y creates alias Contains instance vars Default is null
Example 5 null 7 ab Link Data
Example 5 null 7 ab Link Data Link a = new Link () a.data = 5 Link b = new Link () b.data = 7 a.link = b
Example 5 null 7 ab Link Data Evaluate a.link a.data a.link.data b.link a.link.link b.data
Example 5 null 7 ab Link Data What if you do b.link = a b.data = a.data b.link = a.link a.data = b.data
Aliasing Exercises Link a = new Link (); Link b = new Link (); Link c = new Link (); Link d = a; a.data = 2; b.data = 3; c.data = 4; d.data = 5; a.link = b; b.link = c; c.link = a; a.link.data = 6; b.link.link.data = 7; print(d.data);
Avoiding Aliasing One style of programming avoids issues involving aliasing: non-destructive (function) programming. Once you create structure, you never change it. This allows you to ignore the difference between objects and values
Embracing Aliasing If aliasing is present, you need to know that changes are seen by all objects sharing a reference (pointer) to an object. This might be just what you want! You need to be VERY aware of where pointers point.