1 / 6

Data Lifetime

Data Lifetime. There are three possible lifetime patterns for Java data. Static Data. The data is created when the program begins execution and is retained until the program completes. Automatic Data.

Download Presentation

Data Lifetime

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. Data Lifetime There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program completes. Automatic Data The data is created when a method begins to execute and is deallocated when the method returns. Dynamic Data The data is created and deallocated under program control. Recall that reference data has two parts:  the reference (address) and  the object In Java a garbage collector recovers any unused dynamic data.

  2. Example public class Pointless { private Point pt; private static Point pt109; public Pointless() { pt = new Point(1, 2); pt109 = new Point(0, 109); } public void slide(int dx) { pt= new Point(pt.getX()+dx, pt.getY()); } public void assignTo109(Point p) { pt109 = pt; pt = p; } public Point getPt() { return pt; } public Point getPt109() { return pt109; } } public class Point { private int x; private int y; public Point(int a, int b) { x = a; y = b; } public int getX() { return x; } public int getY() { return y; } public Point midPt(Point p) { int midX = (x+p.x)/2; int midY = (y+p.y)/2; return new Point(midX,midY); } }

  3. trace the following... public class Driver { public static void main(String args[]) { new Driver(); } public Driver() { Point p1, p2 , p3; p1 = new Point(10, 20); p2 = new Point(30, 30); p2 = p1.midPt(p2); p3 = p2; Pointless ptless1, ptless2; ptless1 = new Pointless(); ptless2 = new Pointless(); ptless1.slide(3); ptless1.assignTo109( new Point(7, 1) ); ptless2.assignTo109( new Point(3, 4) ); System.out.println( ptless1.getPt109().getX() ); System.out.println( ptless2.getPt109().getX() ); } }

  4. trace the following by checking out memory allocation... public class Driver { private int intVar = 1; public Driver() { aMethod(2); System.out.println(StatThing.statVar); StatThing thing1 = new StatThing(3, 4); StatThing thing2 = new StatThing(5, 6); } private void aMethod(int j) { int localVar = j; System.out.println(localVar); } public static void main(String args[]) { Driver d = new Driver(); } } public class StatThing { private int intVar = 100; public static int statVar = 200; public StatThing(int k, int n) { intVar = intVar + k; statVar = statVar + 200; System.out.println(intVar); System.out.println(statVar); } }

  5. public okay for this example Referencing Data of Like Type public class IntCell { public int content; public IntCell link; public intCell(int a) { content = a; link = null; } public void setLink(IntCell c) { link = c; } public int setContent(int j) { content = j; } } /* in some other class... IntCell cell1, cell2; cell1 = new IntCell(2); cell2 = new IntCell(3); cell2.setLink( cell1 );

  6. A More Extensive Example IntCell cell1 = new IntCell(11); IntCell cell2 = new IntCell(22); IntCell cell3 = new IntCell(33); cell2.setLink( cell1 ); cell1.setLink( cell3 ); cell1 = null; cell3 = null; System.out.println( cell2.content ); System.out.println( cell2.link.content ); System.out.println( cell2.link.link.content ); cell2.link.setContent(44); cell2.link.link.setContent(55); cell2.link.link.setLink(cell2); System.out.println( cell2.link.link.link.content );

More Related