1 / 16

Reference Details (copying, comparing)

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.

emike
Download Presentation

Reference Details (copying, comparing)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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! • Let’s 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. 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?

  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. 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?

  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. 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:

  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

More Related