Java Q&A
by Alexandre Sieira Vilar

Listing One
class A {
   public:
          int i;
};
// Implicit memory allocation and deallocation.
int function1 ()
{
          A a;
          a.i = 0;
}
// Explicit memory allocation and deallocation.
int function2 ()
{
          A *a = new A();
          a->i = 0;
          delete a;
}


Listing Two
import java.io.*;
import java.awt.geom.*;

public class GeomParser
{
          // Reads a point from the given input stream and stores it in
          // parameter 'pt'.
          public void readPoint( DataInputStream dis, Point2D pt )
          {
                    double x, y;
                    x = dis.readDouble();
                    y = dis.readDouble();
                    pt.setLocation(x, y);
          }
          // Reads a rectangle from the given input stream and stores it in
          // parameter 'rect'.
          public void readRectangle( DataInputStream dis, Rectangle2D rect )
          {
                    Point2D p1 = new Point2D.Double(),
                             p2 = new Point2D.Double();
                    readPoint(dis, p1);
                    readPoint(dis, p2);

                    rect.setRect(0.0, 0.0, 0.0, 0.0);
                    rect.add(p1);
                    rect.add(p2);
          }
}

Listing Three
import java.io.*;
import java.awt.geom.*;

public class GeomParser
{
          Point2D p1 = new Point2D.Double();
          Point2D p2 = new Point2D.Double();

          // Reads a point from the given input stream and stores it in
          // parameter 'pt'.
          public void readPoint( DataInputStream dis, Point2D pt )
          {
                    double x, y;
                    x = dis.readDouble();
                    y = dis.readDouble();
                    pt.setLocation(x, y);
          }
          // Reads a rectangle from the given input stream and stores it in
          // parameter 'rect'.
          // Method has to be made synchronized to avoid thread contention
          // over objects 'p1' and 'p2'.
          public synchronized void readRectangle( DataInputStream dis, 
                                                           Rectangle2D rect )
          {
                    readPoint(dis, p1);
                    readPoint(dis, p2);

                    rect.setRect(0.0, 0.0, 0.0, 0.0);
                    rect.add(p1);
                    rect.add(p2);
          }
}


Listing Four
import java.io.*;
import java.awt.geom.*;

public class GeomParser
{
          // Reads a point from the given input stream and stores it in
          // parameter 'pt'.
          public void readPoint( DataInputStream dis, Point2D pt )
          {
                    double x, y;
                    x = dis.readDouble();
                    y = dis.readDouble();
                    pt.setLocation(x, y);
          }
          // Reads a rectangle from the given input stream and stores it in
          // parameter 'rect'.
          public void readRectangle( DataInputStream dis, Rectangle2D rect )
          {
                    Point2D p1, p2;
                    // Obtain instances from object pool.
                    p1 = (Point2D) 
                         ObjectPool.allocate("java.awt.geom.Point2D$Double"),
                    p2 = (Point2D) 
                         ObjectPool.allocate("java.awt.geom.Point2D$Double");
                    readPoint(dis, p1);
                    readPoint(dis, p2);

                    rect.setRect(0.0, 0.0, 0.0, 0.0);
                    rect.add(p1);
                    rect.add(p2);

                    // Objects are no longer needed, release them.
                    ObjectPool.release(p1);
                    ObjectPool.release(p2);
          }
}

Listing Five
import java.awt.geom.*;
import tecgraf.objpool.*;
public final class Point2D_Double_ClassManager
extends ClassManager
{
          private Point2D_Double_ClassManager()
          {
                    super();
          }
          public Object newInstance()
          {
                    return new Point2D.Double();
          }
          static {
                    // Installs class manager for java.awt.geom.Point2D.Double
                    ObjectPool.setClassManager(
                             "java.awt.geom.Point2D$Double",
                             new Point2D_Double_ClassManager()
                    );
          }
}





1


