Transcript - EdShare

Coming up
•
•
•
•
Implementation vs. Interface
The Truth about variables
Comparing strings
HashMaps
Lecture 9
Shhh, no talking in the library
OO
Coming up
•
•
•
•
Implementation vs. Interface
The Truth about variables
Comparing strings
HashMaps
Implementation and interface
• The API describes
– what a class does (interface of the class)
– not how a class does it (implementation of a class)
• When you write classes, you write the
implementation of those classes
Library
• The java library is full of helpful classes
• Like ArrayList
•
•
•
•
What does the inside of an ArrayList look like?
How does it handle the resizing?
How does it know when the throw an error?
How does it handle renumbering when removing
elements?
OO
Interface
• All we need to know to use the ArrayList class,
and the other library classes is what their
interface is
• Interface is how we interact with a class
– what methods to call
– what they will return
– etc
Importing
• Library classes must be imported using an
import statement
– except classes from java.lang.
• i.e. String – look it up on API
import java.util.ArrayList;
public class myClass{
ArrayList<String> arrl;
arrl = new ArrayList<String>;
public static void main(String[] args){
}
}
Importing packages
• Classes are organised in packages.
• Single classes may be imported:
import java.util.ArrayList;
• Whole packages can be imported:
import java.util.*;
Strings
• Strings are actually objects
– Did you notice we always use a capital S like other
classes?
• You don’t need to import them – they are
from the automatically imported from
java.lang.*;
• As is System as in System.out.println()
Coming up
• Implementation vs. Interface
• The Truth about variables
– Well, an abstract version of the truth anyway
• Comparing strings
• HashMaps
OO
Have you ever..
• Accidently tried to output (print) an object?
int[] array = new int[10];
System.out.println(array);
You’ll get something like:
[I@955cd5
OO
What’s that?
• This is the address in memory that the
computer uses to store objects
• in some other langauges (i.e. C++) you can
manipulate those addresses and their
contents directly
• It’s time to look into the “variables as cups”
anaology in a bit more detail
So far...
• We’ve said
– A variable is a cup
– A primitive fits into a cup
– A remote control to an object fits into a cup
The Truth
• The “cup” is a bit of storage on a silicone chip in
the computer somewhere
• By giving the storage cups names, the computer
associates the name of the cup with the address
in memory where the storage is.
• int age = 7; really means
– “when the user types ‘age’ they really mean the
contents of memory area
010111011010001010111001110101010”
The Truth
• A bit pattern goes into the storage
– 0100100111110101110101000011011000010011
• This bit pattern can either represent:
– A number, character or other primitive value
– The address in memory of something else, an
object
OO
So what?
• Why is this important?
• It’s important to know for comparing Strings.
Coming up
•
•
•
•
Implementation vs. Interface
The Truth about variables
Comparing strings
HashMaps
A word about comparing Strings
if(input == “hello") {
//code here
}
if(input.equals(“hello")) {
//code here
}
• Strings should always be compared with .equals
OO
OO
•
•
•
••
•
identity vs equality
name1==name2
name1 == name2
False,same
True,
different
address
addresses
name1.equals(name2)
name1.equals(name2)
True, same characters
True,
value
False,same
different
value
Memory
Harry
name1
String
Harry
Ron
name2
String
Coming up
•
•
•
•
Implementation vs. Interface
The Truth about variables
Comparing strings
HashMaps
HashMap
• One of the most useful classes in the library is the
HashMap
• It:
– takes an object (The key)
– looks at the internal properties within it
– uses algorithms to compute a number based on those
properties. This is the object’s hash code
– stores the object in something like an array in the
index given by the hash code
Then...
• Then the HashMap associates the second
object (the value) with the key object
• This is useful
Maps
4073
51
Alfie
Memory
• Maps are a collection
type that map a key
to a value
Map: Name -> Phone number
Jenny
7634
12
• add(“Alfie”,“407351”);
• and so on
Lookup
• Lookup is the act of supplying the key and having
the value returned
String num = myHashMap.get(“Alfie”);
Memory
myHashMap<String, Integer>
Alfie
4073
51
• HashMaps are used in upcoming labs...
– you will come across Sets. They are a collection
like any other, you can use the for-each loop with
them etc
– What happens if you add a key and a value, and
then add the same key again, but with a different
value?
– Check out HashMaps in the API
Covered today
•
•
•
•
Implementation vs. Interface
The Truth about variables
Comparing strings
HashMaps