Download presentation
Presentation is loading. Please wait.
Published byReginald Jackson Modified over 8 years ago
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?
6
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
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 arrl; arrl = new ArrayList ; 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 –W–Well, an abstract version of the truth anyway Comparing strings HashMaps
11
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
12
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
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
So what? Why is this important? It’s important to know for comparing Strings. OO
17
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps
18
A word about comparing Strings if(input == “hello") { //code here } if(input.equals(“hello")) { //code here } Strings should always be compared with.equals OO
19
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
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 Maps are a collection type that map a key to a value add(“Alfie”,“407351”); and so on Memory Map: Name -> Phone number 4073 51 Alfie 7634 12 Jenny
24
Lookup Lookup is the act of supplying the key and having the value returned String num = myHashMap.get(“Alfie”); Memory myHashMap 4073 51 Alfie
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.