Archive
May 18, 2012 - 2:56 pm
Though the actual working of garbage collector is internal to JVM but there are some algorithms which can be used by any garbage collector. The two famous algorithms are described here:
a) Mark and Sweep: This algorithm works by suspending all the currently executing user threads and starting to examine all the objects for a “in use” flag. Each object in this algorithm is made to have a “in use” flag which can be set or cleared. During the first run, objects which are not reachable are marked as not in use and their “in use” flag is cleared but for other objects this flag is set. In the next run if any objects which are found to have “in use” flag cleared are garbage collected. Also all the other objects (“in use” flag set) are scanned to check if they are reachable and their flags are cleared if they are not reachable. The objects which get their “in use” flag as cleared in this run are candidates for garbage collection in the next run.
As we can see that in every run some objects are marked for garbage collection and some are garbage collected, hence
the name of the algorithm “Mark and Sweep”.
b) Copying Collector: This algorithm works by dividing memory into two portions viz “new objects” space and “garbage candidate” space. Initially the objects are allocated in “new objects” space and once this space is full, all the reachable objects are identified and moved to “garbage candidate” space. All non reachable objects are garbage collected. After each cycle, the “garbage collected” space becomes “new objects” space and vice versa.
Besides these simple algorithms, the advanced and efficient algorithms include parallel copying and concurrent mark-sweep
- 2:54 pm
For a language to pure object oriented, following features must be supported by the language:
1) Encapsulation/Data Hiding
2) Inheritance
3) Polymorphism
4) All pre-defined types are objects
5) All user-defined types are objects
6) All operations are defined as method calls on objects
Java is not a pure object oriented language because:
1) Java supports primitive data types (char, int, float, double and long etc.) which are… Continue reading
- 2:52 pm
Class file magic number is the number which is written at the starting of each class file. The java class loader checks for this number in order to identify whether a file is really a class file. This magic number is CAFEBABE.
One can verify the existence of magic number by opening any class file using a hex viewer. Following is the sample output for a test class… Continue reading
- 2:50 pm
Unicode is a standard to provide consistent encoding standard of characters for use in the text. Unicode uses 16 bits for encoding each character and hence has a large collection of characters. The visual attributes like font, size and height is left to the application/program using the Unicode standard. The Unicode standard has been widely accepted in Java, XML and .Net.
This standard was started by a group of… Continue reading
- 2:06 pm
The rules of overloading are:
1) The name of method should be same for the overloaded methods.
2) The methods should differ either in the number of arguments or the type of arguments.
Example Code:
1
public class Test1{
Number methodA(int a){
return new Integer(a);
}
Integer methodA(long a){
return new Integer((int)a);
}
Integer… Continue reading
- 10:00 am
When using the file based logging, there are many points to consider including the appender, rolling frequency, log level etc. In this log4j tutorial, we shall see how a typical log4j file appender looks like and how can we the logger to use different file appenders each appending logs to different log files. .
The following logging.properties file configures two rolling file appenders and then configures them… Continue reading
- 8:43 am
Since the static members are associated with the class and same single method is shared among all the instances of the class, there is no question of resolving method call at runtime based on the object on which method is invoked. Hence the subclass’s static method (a static method having same signature as super class’s static method) makes the super class’s static method as hidden. Thus irrespective of whether the… Continue reading