DuraSoft  TechLet 09, 2002

In this issue
Welcome to the September 2002 TechLet. If you have received this through some one you know and would like to subscribe directly for our future issues, please send an email to techlet@durasoftcorp.com with a subject subscribe. This subscription currently is absolutely free.

In this issue, we have a detailed article on Garbage Collection in Java and .NET, and how to create/avoid memory leaks.

In depth
How to create/avoid memory leaks in Java and .NET? Java and .NET provide run time environment for managed code, and Automatic Garbage Collection (AGC) is one of the features that every programmer is eager to utilize and benefit from. One of the myths with managed code is that you don't have to worry about memory leaks. However, the truth is that even though these managed code run time environments provide AGC, developers are still required to worry about a few details. In this article, we will first discuss the AGC process of Java and what it takes to clean-up the resources. Then we address how easy it is to create memory leaks while programming in Java and .Net. Finally, we will discuss the AGC process in .NET, and how the resource clean-up is addressed in .NET. The complete article can be found at http://www.durasoftcorp.com/download
Quiz corner
This month's quiz is exclusively for those who consider themselves savvy in C++. In the June article, we discussed about the issues with implementing the equals method in Java. The solution pretty much addresses how you can solve the problem in a language (like Java, .NET languages) that supports reflection. It got me thinking about how one can solve the problem of equals (operator==) in C++ efficiently. So, here is the question to you.

Can you implement the operator== for the code given below such that it produces the correct result?

#include 
using namespace std;

class Point
{
	public:
		// Showing only minimal code.
		virtual ~Point() {}
		Point(int px, int py) : x(px), y(py) {}
		virtual bool operator==(const Point& other) const
		{
			// Your code?
		}
	private:
		const int x;
		const int y;
};

class ColorPoint : public Point
{
	public:
		ColorPoint(int px, int py, int clr) : Point(px, py)
		{
			color = clr;
		}
	
		virtual bool operator==(const Point& other) const
		{
			// Your code?
		}
	private:
		int color;
};

void main()
{
	ColorPoint p1(1, 2, 5);
	Point p2(1, 2);
	ColorPoint p3(1, 2, 6);
	ColorPoint p4(1, 2, 5);

	cout << (p1 == p2) << endl;
	cout << (p2 == p1) << endl;
	cout << (p1 == p2) << endl;
	cout << (p2 == p3) << endl;
	cout << (p3 == p1) << endl;
	cout << (p1 == p4) << endl;
}				

Quiz from the past issue

What is the result of executing the following code (try to answer without running it of course)? The answer will be in the next issue.

 
	class Employee
	{ 
		public Employee()  { work(); } 
		public void work(){ System.out.println("Employee working"); }
	}
		
	class Room
	{
		public void openWindow() {}
	}
	
	class ExecutiveRoom extends Room {}
	
	class Manager extends Employee
	{
		private Room theRoom;
		public Manager(int level)
		{
			if (level == 1) 
				theRoom = new Room();
			else
				theRoom = new ExecutiveRoom();
		}
		public void work()
		{
			theRoom.openWindow();
			super.work();
		}
	}
	
	public class Test
	{
		public static void main(String[] args)
		{
			Employee emp = new Employee();
			emp.work();
			Manager mgr = new Manager(2);
			mgr.work();
		}
	}

Answer!

Polymorphism in Java works differently than in C++ for calls from within a constructor. When an object of Manager is being created, the constructor of the Employee class is called before the constructor of the Manager class is executed. However, from within the constructor of the Employee class, when the work method is called, polymorphically the work method of the Manager is called instead of the work method of the Employee. Since the member "theRoom" of Manager has not yet been assigned a reference to an instance of Room or ExecutiveRoom, you get a NullPointerException. Solution - the safest way to code is to always call only the final or static (basically, non-virtual) functions from within the constructor.

 

Message from us
If you like any question to be addressed or topic to be discussed, please send us an email to techlet@durasoftcorp.com. At the end of the year, one person will be randomly selected from the list of respondents for a special prize. We would like to hear from you.

Please send suggestions, corrections and comments to techlet@durasoftcorp.com.

Thus spake...
It is the mark of an educated mind to be able to entertain a thought without accepting it.  - Aristotle.