Coming%20up - PowerPoint PPT Presentation

About This Presentation
Title:

Coming%20up

Description:

name1 == name2. False, different addresses. name1.equals(name2) False, ... name1.equals(name2) True, same characters. OO. Coming up. Implementation vs. Interface ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 27
Provided by: ter80
Category:
Tags: 20up | coming | name1

less

Transcript and Presenter's Notes

Title: Coming%20up


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
Interface
OO
  • 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
  • ArrayListltStringgt arrl
  • arrl new ArrayListltStringgt
  • public static void main(String args)

8
Importing packages
  • Classes are organised in packages.
  • Single classes may be importedimport
    java.util.ArrayList
  • Whole packages can be importedimport
    java.util.

9
Strings
  • Strings are actually objects
  • Did you notice we always use a capital S like
    other classes?
  • You dont 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
Have you ever..
OO
  • Accidently tried to output (print) an object?
  • int array new int10
  • System.out.println(array)
  • Youll get something like
  • I_at_955cd5

12
Whats that?
OO
  • 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
  • Its time to look into the variables as cups
    anaology in a bit more detail

13
So far...
  • Weve 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 01011101101000101011100111
    0101010

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?
OO
  • Why is this important?
  • Its 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
identity vs equality
OO
  • name1name2
  • False, different addresses
  • name1.equals(name2)
  • True, same value
  • name1name2
  • True, same address
  • name1.equals(name2)
  • True, same characters

Memory
  • name1 name2
  • False, different addresses
  • name1.equals(name2)
  • False, different value

Harry
Ron
Harry
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 objects 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
407351
Alfie
  • Maps are a collection type that map a key to a
    value
  • add(Alfie,407351)
  • and so on

Memory
Map Name -gt Phone number
763412
Jenny
24
Lookup
  • Lookup is the act of supplying the key and having
    the value returned
  • String num myHashMap.get(Alfie)

Memory
myHashMapltString, Integergt
407351
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
Write a Comment
User Comments (0)
About PowerShow.com