Download presentation
Presentation is loading. Please wait.
Published byJarvis Winders Modified over 9 years ago
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
What’s 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 <- 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 wouldn’t 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. data next Big record data next Big record
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! Let’s make a small class to use as an illustration. This class will hold info about boxes.
7
class Box 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 OK? len wid
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? b1 b2 box(3, 4)
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("I’m sooooo confused!"); Your final answer? b1 b2 box(3, 4)
11
Copying? Box b1 = new Box(3, 4); Box b2 = new Box(5, 7); b2 = b1; Where did the other box go? b1 b2 box(3, 4) box(5, 7) b1 b2 box(3, 4)
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 b1 = new Box(3, 4); Box b2 = b1.duplicate(); Box b3 = b2; b2.setWid(8); System.out.println(b3.getWid()); b2 = null; b1 b2 box(3, 4) box(3, 8) b3 b1 box(3, 4) b2 box(3, 8) b3 b1 box(3, 4)
14
Reference Details: int x; Creates an int primitive Queue q; Creates a reference (pointer) to a queue object. Note: No object has been created q = new Queue(); q is now pointing to a Queue object or more correctly q now has a reference to a queue object. Queue q2 = new Queue(); The q2 reference can be created and initialized in one step. It is now pointing at a queue object.
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; else return false; } public Box duplicate() { return new Box(getLen(), getWid()); } public static void main(String args[]) { 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!"); if(b1.equals(b2)) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); Box b3 = b1.duplicate(); if(b1.equals(b3)) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); } } // Box
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.