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?
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 OO
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 arrl; arrl = new ArrayList ; 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 –W–Well, an abstract version of the truth anyway Comparing strings HashMaps
Have you ever.. Accidently tried to output (print) an object? int[] array = new int[10]; System.out.println(array); You’ll get something like: 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 OO
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 ”
The Truth A bit pattern goes into the storage – This bit pattern can either represent: – A number, character or other primitive value – The address in memory of something else, an object
So what? Why is this important? It’s important to know for comparing Strings. OO
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
identity vs equality Memory name1 String Harry Ron name2 String Harry name1 == name2 False, different addresses name1.equals(name2) False, different value name1==name2 False, different addresses name1.equals(name2) True, same value name1==name2 True, same address name1.equals(name2) True, same characters OO
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 Maps are a collection type that map a key to a value add(“Alfie”,“407351”); and so on Memory Map: Name -> Phone number Alfie Jenny
Lookup Lookup is the act of supplying the key and having the value returned String num = myHashMap.get(“Alfie”); Memory myHashMap Alfie
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