1 / 26

Coming up

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.

fancy
Download Presentation

Coming up

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coming up • Implementation vs. Interface • The Truth about variables • Comparing strings • HashMaps

  2. Lecture 9 Shhh, no talking in the library OO

  3. Coming up • Implementation vs. Interface • The Truth about variables • Comparing strings • HashMaps

  4. 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

  5. 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? Implementation

  6. 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

  7. 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){ } }

  8. Importing packages • Classes are organised in packages. • Single classes may be imported:import java.util.ArrayList; • Whole packages can be imported:import java.util.*;

  9. 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()

  10. Coming up • Implementation vs. Interface • The Truth about variables • Well, an abstract version of the truth anyway • Comparing strings • HashMaps

  11. 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

  12. 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

  13. 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

  14. 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”

  15. 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

  16. OO So what? • Why is this important? • It’s important to know for comparing Strings.

  17. Coming up • Implementation vs. Interface • The Truth about variables • Comparing strings • HashMaps

  18. A word about comparing Strings Tests Identity if(input == “hello") { //code here } if(input.equals(“hello")) { //code here } • Strings should always be compared with.equals Tests Equality OO

  19. OO identity vs equality • name1==name2 • False, different addresses • name1.equals(name2) • True, same value • name1==name2 • True, same address • name1.equals(name2) • True, same characters • name1 == name2 • False, different addresses • name1.equals(name2) • False, different value Memory Harry Ron Harry name1 name2 String String

  20. Coming up • Implementation vs. Interface • The Truth about variables • Comparing strings • HashMaps

  21. 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

  22. Then... • Then the HashMap associates the second object (the value) with the key object • This is useful

  23. Maps Alfie 407351 • Maps are a collection type that map a key to a value • add(“Alfie”,“407351”); • and so on Memory Map: Name -> Phone number Jenny 763412

  24. Lookup • Lookup is the act of supplying the key and having the value returned String num = myHashMap.get(“Alfie”); Memory myHashMap<String, Integer> Alfie 407351

  25. 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

  26. Covered today • Implementation vs. Interface • The Truth about variables • Comparing strings • HashMaps

More Related