Mysterious Pointers.pptx

advertisement
As you arrive…
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;
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());
What does this code print?
DnaStrand a = new DnaStrand();
a.setStrandData("aaaa");
DnaStrand b = a;
b.addBasePair("t");
System.out.println(b.getStrandData());
1.
2.
3.
4.
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
1. Correctly anticipate tricky pointer
manipulations and understand the
difference between changing with a
myVariable.method() and changing with ‘=‘
2. 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 = new DnaStrand();
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. MyClass foo = null;
foo.doSomething();) you’ll generate a
NullPointerException and your program will end
public static void randomFunction()
{
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> otherList = list;
list.add(44);
otherList.add(55);
}
What does otherList contain at the end of randomFunction?
A. [44]
B. [55]
C. [44,55]
D. None of these; the function won’t compile because it is static
public static void randomFunction2()
{
String a = "Hello";
String b = "Goodbye";
b = a;
String c = a.concat(" CS100");
}
What is true at the end of randomFunction2?
A. b contains “Hello CS100”
B. b contains “Goodbye” because strings are immutable
C. b contains“Hello” and a will contain “Hello CS100” because string
are immutable
D. 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 = new DnaStrand();
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:
public DnaStrand(String data)
{
myData = data;
}
It’s called when an object is created with new:
DnaStrand myVariable = 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”);
NamedDna myVar = new NamedDna(“Mikes Strand”, d);
When this works, go ahead and submit the
code via Ambient.
Summing up!
1. Don’t be fooled by tricky code. All (nonprimitive) variables are pointers. Avoid
null! Remember the rules of pointers!
2. Write constructors for your objects, and
know where to look when you see them
called
Download