160 likes | 169 Views
Explore the fundamental concepts and applications of pointers and references in programming, uncovering their significance in memory management and efficiency. Learn how Java eliminates the complexity of pointers and leverages references for object manipulation.
E N D
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
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) ...
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!
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. Big record data Big record data next next
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.
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?
box(3, 4) b1 box(3, 4) b2 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?
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(); } }
box(3, 4) b1 box(3, 4) b2 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?
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
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?
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
Creates an int primitive int x; 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. Reference Details:
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