Reference Details copying, comparing - PowerPoint PPT Presentation

About This Presentation
Title:

Reference Details copying, comparing

Description:

... capabilities in pseudocode are relatively tame as compared to languages such as ... In fact, pointers (and their misuse) are considered by some experts to ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 17
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Reference Details copying, comparing


1
Reference Details(copying, comparing)
2
References
  • You were introduced to the concept of pointers
    earlier this term.
  • The pointer capabilities in pseudocode are
    relatively tame as compared to languages such as
    C which was originally developed for system
    programming
  • In fact, pointers (and their misuse) are
    considered by some experts to be a very dangerous
    aspect of programming.
  • The designers of Java decided to implement their
    language and eliminate many of the "dangerous"
    aspects of pointers. They even decided to call
    them something else references

3
Whats all the fuss?
  • Pointers (and references) are used for a very
    simple reasons efficiency
  • Pointers save time
  • Pointers save space
  • Example
  • Procedure add(cur iot in/out Ptr toa Node,
  • dataIn iot in SomeBigRecType)
  • ...
  • Function get returnsa SomeBigRecType
  • (cur iot in Ptr toa Node)
  • ...

4
Efficient?
  • The add procedure asks us to pass in a big record
    of some type. This (as we have defined it) would
    require us to make a local copy inside the
    module.
  • Then with a line of code like
  • temp.data lt- datain
  • We would make another copy!
  • The get module would reverse the process. It
    might again copy the record one or more times.
  • All this space...all this time to copy!

5
The Real World
  • In the real world code would be much more likely
    to simply make some space for the data once and
    from then on just pass pointers to the data.
  • So the linked list Node wouldnt really contain a
    data record and a next pointer.
  • It would contain a pointer to a data record AND a
    pointer to the next Node.

Big record
data
Big record
data
next
next
6
Java References
  • Java takes advantage of this concept by
    manipulating all objects (which are actually
    chunks of memory on the heap) using references.
  • This may cause some confusion!
  • Lets make a small class to use as an
    illustration.
  • This class will hold info about boxes.

7
class Box
len
  • class Box
  • private int len
  • private int wid
  • public Box(int len, int wid)
  • setLen(len)
  • setWid(wid)
  • public void setLen(int len) this.len len
  • public void setWid(int wid) this.wid wid
  • public int getLen() return len
  • public int getWid() return wid
  • public int getArea() return getLen()getWid()
  • public String toString()
  • return ("Rec l "getLen()", w "getWid())
  • // Box

wid
OK?
8
Confusion?
  • Box b1 new Box(3, 4)
  • Box b2 new Box(3, 4)
  • if(b1 b2)
  • System.out.println("The boxes are obviously
    equal!")
  • else
  • System.out.println("I'm sooooo confused!")
  • Your final answer?

9
Solution
  • We need a method that compares two boxes!
  • (And it has to be in the Box class!)
  • public boolean equals(Box b)
  • if(getLen() b.getLen() getWid()
    b.getWid())
  • return true
  • else
  • return false

class Box private int len private int
wid public Box(int len, int wid)
setLen(len) setWid(wid) public void
setLen(int len) this.len len public void
setWid(int wid) this.wid wid public int
getLen() return len public int getWid()
return wid public int getArea() return
getLen()getWid() public String toString()
return "(Rec l "getLen()", w
"getWid()
10
Solution
  • Box b1 new Box(3, 4)
  • Box b2 new Box(3, 4)
  • if(b1.equals(b2))
  • System.out.println("The boxes are obviously
    equal!")
  • else
  • System.out.println("Im sooooo confused!")
  • Your final answer?

11
Copying?
box(3, 4)
b1
  • Box b1 new Box(3, 4)
  • Box b2 new Box(5, 7)
  • b2 b1
  • Where did the other box go?

box(5, 7)
b2
box(3, 4)
b1
b2
12
Duplicating?
  • What if I want to make another identical box?
  • One way
  • Box b1 new Box(3, 4)
  • Box b2 new Box(b1.getLen(), b1.getWid())
  • Another way...add a duplicate method into the Box
    class!
  • public Box duplicate()
  • return new Box(getLen(), getWid())
  • Now we can say Box b2 b1.duplicate()

cloning?
13
Copying?
box(3, 4)
b1
  • Box b1 new Box(3, 4)
  • Box b2 b1.duplicate()
  • Box b3 b2
  • b2.setWid(8)
  • System.out.println(b3.getWid())
  • b2 null

box(3, 4)
b1
box(3, 8)
b2
b3
box(3, 4)
b1
box(3, 8)
b2
b3
14
Reference Details
15
Complete Box Class
  • class Box
  • private int len
  • private int wid
  • public Box(int len, int wid)
  • setLen(len)
  • setWid(wid)
  • public void setLen(int len) this.len len
  • public void setWid(int wid) this.wid wid
  • public int getLen() return len
  • public int getWid() return wid
  • public int getArea() return getLen()getWid()
  • public String toString()
  • return "(Rec l "getLen()", w "getWid()
  • public boolean equals(Box b)
  • if(getLen() b.getLen() getWid()
    b.getWid())
  • return true

16
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com