Saturday, 28 August 2010

Customising Serialization process

You can customize the process of serialization by two ways :

1. write your own readObject and writeObject methods in your class (which of course implements serializable interface. )

2. Implement Externalizable interface and override methods writeExternal and readExternal.

Tuesday, 24 August 2010

What the differences are between the Heap Size and the MaxPermSize ?

First off, what is the JVM Heap? The heap is memory that's been allocated to store Java classes and objects. The JVM heap is split up into several areas and newer/older objects are shuffled between the sections based on their age and use. The shuffling of object occurs during Garbage Collection, a process which also discards objects which are no longer used.
That's a description of the heap, but what about this other setting set with -XX:MaxPermSize? Turns out this sets the size for something called the "Permanent Generation".
The permanent generation is used to hold reflective of the VM itself such as class objects and method objects. These reflective objects are allocated directly into the permanent generation, and it is sized independently from the other generations. Generally, sizing of this generation can be ignored because the default size is adequate. However, programs that load many classes may need a larger permanent generation.
So the permanent generation contains information about the objects in the heap. Ah-ha! Now we can start to understand how these two numbers are related to each other. The heap stores the objects, and the permanent generation keeps track of information about the objects. Consequently, the more objects there are in the heap, the more information there is to be tracked about them, and the larger the permanent generation needs to be.

Saturday, 21 August 2010

Java File without a "class" or "interface" in it.

Yes you can write a Java file without a class or interface in it.

This File is "package-info.java"

The code goes as follows for this File:-
==============================================
/**
* documentation comments...
*/
@Annotation1(...)
package sample;

==============================================
Now compile this file...it will compile and will produce class file.But Question remains how it compiles this file and what is the use of this file??

This file is meant for any package level comments and/or annotations.
As Java Files does not support these over Package statement.

When the compiler encounters package-info.java file, it will create a synthetic interface,package-name.package-info. The interface is called synthetic because it is introduced by the compiler and does not have a corresponding construct in the source code. This synthetic interface makes it possible to access package-level annotations at runtime. The javadoc utility will use the package-info.java file if it is present, instead of package.html, to generate documentation.


In system.out.println what is out and println ? Can this be redirected to a file ? How ?

Yes you can set FileOutputStream Object to override default out implementation as follows:
=================================================
File f = new File("testOut.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
PrintStream pos = new PrintStream(fos);
System.setOut(pos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.print("Hello world File");
=================================================