140 likes | 271 Views
FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code you snarfed , if you’d like to play with it in Eclipse): DnaStrand a = new DnaStrand (); a.setStrandData ( " aaaa " ); DnaStrand b = a;
E N D
FIRST:Grab the handout SECOND: Snarf the code for today’s class THIRD:Explain what’s going on in this code (It’s Example 1 in the code you snarfed, if you’d like to play with it in Eclipse): DnaStrand a = newDnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); //prints aaaat System.out.println(a.getStrandData()); FORTH:determine what would happen if the final line were changed to: System.out.println(b.getStrandData()); As you arrive…
What does this code print? DnaStrand a = newDnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); System.out.println(b.getStrandData()); t aaaa aaaat Nothing, this could would cause an error
The Mysteries of Pointers By the end of the class, you should be able to Correctly anticipate tricky pointer manipulations and understand the difference between changing with a myVariable.method() and changing with ‘=‘ Write constructors for your objects
In the Optional Textbook • Your book calls pointers “references” • The stuff today will be mostly out of Chapter 3 (especially starting on pg. 54) • Coming up – read Ch 4 and Ch 6 (though you can skip the big extended example in Ch 5 and Ch 6 unless you’re really feeling motivated)
Why does this code work differently? Old Code from the Beginning of Class: DnaStrand a = newDnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); //prints aaaat System.out.println(a.getStrandData()); New similar but differently functioning: String a = new String("aaaa"); String b = a; b = a.concat("t"); //prints aaaa System.out.println(a); //prints aaaat System.out.println(b);
The Rules of Pointers • New objects are only created with “new” • Variables are not objects they are pointers to objects (except for primitives) • By using ‘=‘ we change the object a variable points to. Two variables can point to the same object. • By using myVariable.someFunction() we (may) change the object being pointed at, which affects everything that’s pointing to it • When pointers are newly created, they point to “null” • If you attempt to call a function on something that points to null (e.g. MyClassfoo = null; foo.doSomething();) you’ll generate a NullPointerException and your program will end
publicstaticvoidrandomFunction() { ArrayList<Integer> list = newArrayList<Integer>(); ArrayList<Integer> otherList = list; list.add(44); otherList.add(55); } What does otherList contain at the end of randomFunction? [44] [55] [44,55] None of these; the function won’t compile because it is static
publicstaticvoid randomFunction2() { String a = "Hello"; String b = "Goodbye"; b = a; String c = a.concat(" CS100"); } What is true at the end of randomFunction2? b contains “Hello CS100” b contains “Goodbye” because strings are immutable b contains“Hello” and a will contain “Hello CS100” because string are immutable Both b and a contain “Hello” because strings are immutable
Check out the Example 3 code • Fix it • Remember pointer rule 1!
Constructors DnaStrand a = newDnaStrand(); Looks a lot like a function call, doesn’t it?
Constructors A constructor is a special function. Declare a constructor like this: public ClassName(Type parameter, Type otherParameter) { //Your code here } Example: publicDnaStrand(String data) { myData = data; } It’s called when an object is created with new: DnaStrandmyVariable = new DnaStrand(“ttttta”);
The Rules of Constructors • Constructors always are named the same as their class • They never have a return type • If your class does not have a constructor, one without parameters will be created for you
Constructor Exercise Make a new class NamedDna. Its constructor should should take two parameters: a name and a DnaStrand. Store both of those values in instance variables. Example code: DnaStrand d = new DnaStrand(“tta”); NamedDnamyVar = new NamedDna(“Mikes Strand”, d); When this works, go ahead and submit the code via Ambient.
Summing up! Don’t be fooled by tricky code. All (non-primitive) variables are pointers. Avoid null! Remember the rules of pointers! Write constructors for your objects, and know where to look when you see them called