Click to See Complete Forum and Search --> : Clarification Needed


pkraman
August 16th, 1999, 01:49 PM
Hi,
I am learning JAVA. Basically I am from C++ environment. I have some doubts on JAVA Programming.


1. How the Memory Management is done in JAVA. I am not clear on that.

Consider the Piece of Code

public Object pop()
{
return Storage[index--];
}

If the Value of "index" is 2, the 3rd Object is returned but it will not be eligible for Garbage Collection. We need to write the Statement "Storage[index] = null" to make it free. Then how is the Garbage Collection is powerful. I am not clear on this.

2. Where to write the "main" function if we have more than one class (for example in a Subsystem)

3. How does the Object reference work?. How the contents of Object only gets changed when passed as an argument to a method and not other data types.

4. What is the idea behind the "Transient" variables.

Please clarify my doubts. Appreciate immediate reply.

Thanks in Advance,
Kalyan

unicman
August 17th, 1999, 08:48 AM
1. 3rd object shouldn't be garbage collected obviously. Garbage collector frees only those objects which are not referenced by anyone. And its quite obvious. If u want to free a object u will have to remove all the references. That's why GC is powerful. In C++, programmers need to take care of freeing all the objects. And in some cases, like when u want to pass a object point to other classes and those classes are going to reference the object afterwards, it makes programmer confuse as who will release the object. In this confusion sometimes some objects don't get freed at all and some objects get freed when they r referenced by another class/object. But in JAVA, it takes care of freeying the object. So programmer is free of that burden.

2. U can write it anywhere, but normally u should write it in ur main class (say where u create the main UI for the appln/applet). And in other classes u may have 'main' methods which will let u test that class only. So when testing just that module u can give that class to interpreter instead of loading the whole UI (or whole software) may be.

3. As far as I know all the objects in JAVA r passed by reference and all the basic data-types are passed by value. (That's why writing 'swap' function for basic data-types is not possible in JAVA, u have to create a object somehow and do swapping!).

4. I'm sorry. I'm not sure about it.


- UnicMan
http://members.tripod.com/unicman

pkraman
August 17th, 1999, 09:33 AM
Hi UnicMan,
Thanks for your reply.

Regards,
Kalyan