When to use ArrayList ,LinkedList,Vector?


Consider a list collection. Now lets consider some scenario:

1) the list needs to be modify such that the elements are added only at the end of list, and if required only the last element of the list to be deleted, use ArrayList

2) the list needs to be modify such that the elements can be added at any position of the list, and if required the element can be deleted from any position, use LinkedList

3) the list needs to be threadsafe, use Vector.

4) the list is read only and needs to be iterated frequently, use ArrayList

java.lang.OutOfMemoryError while doing ant build?


While doing an ant build sometimes it happen that a user may get the OutOfMemoryError. This is mainly because the heap size of Java JVM is less. This heap size can be increased. But caution that the heap size of java JVM which is running through Ant has to be increased and not of the JVM of Jdk 1.x .

To increase the heap size of JVM in ant ,open the file ant.cmd present in Ant/bin folder.

Change following line

“%_JAVACMD%” %ANT_OPTS% -classpath “%ANT_HOME%\lib\ant-launcher.jar” “-Dant.home=%ANT_HOME%” org.apache.tools.ant.launch.Launcher %ANT_ARGS% -cp “%CLASSPATH%” %ANT_CMD_LINE_ARGS%

“%_JAVACMD%” –Xms512m -Xmx1024m %ANT_OPTS% -classpath “%ANT_HOME%\lib\ant-launcher.jar” “-Dant.home=%ANT_HOME%” org.apache.tools.ant.launch.Launcher %ANT_ARGS% -cp “%CLASSPATH%” %ANT_CMD_LINE_ARGS%

why inner class can access only final variable?


 

Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren’t final then the copy of the variable in the method could change, while the copy in the local class didn’t, so they’d be out of synch.

Anonymous inner classes require final variables because of the way they are implemented in Java. An anonymous inner class (AIC) uses local variables by creating a private instance field which holds a copy of the value of the local variable. The inner class isn’t actually using the local variable, but a copy. It should be fairly obvious at this point that a “Bad Thing”™ can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems. In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the AIC as final (i.e., unchangeable). This guarantees that the inner class’ copies of local variables will always match the actual values.

Polymorphism


What is polymorphism?

Polymorphism is one of the highly used concept of OOPS . It gives us the ultimate flexibility in extensibility. Polymorphism is a term that describes a situation where one name may refer to different methods. In java there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).

When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class. Overloading occurs when several methods have same names with

·        Different method signature and different number/type of parameters.

·        Same method signature but different number of parameters.

·        Same method signature and same number of parameters but of different type

 

int add(int a,int b)

     float add(float a,int b)

     float add(int a ,float b

     void add(float a)

     int add(int a)

     void add(int a)                 //error conflict with the method int add(int a)

 

Overloading is determined at the compile time.

 

Use of final keyword


I have seen that final keyword mainly can be used with

1)      Class level variable

2)      method

3)      class

4)      Objects

 

1) If a final is assigned to a variable , the variable becomes a constant. It means that the value of variable once assigned  cannot be changed. The variable behaves like a

final int i=0;

i =5; // error

 

2) If a final is assigned to a method than it cannot be overridden in its child Class.

 

Class Parent {

 

Public final void print(){

 

            System.out.println(“Inside”);

}

}

 

Class Child extends Parent{

 

Public final void print(){             // error cannot override final method

 

            System.out.println(“Inside”);

}

}

 

3) If a class is made as final, then no other class can extend it and make it as parent class. Eg String,Integer

 

4) final objects are instantiated only once. i.e

 

final Map map = new HashMap();

 

map.put(“key”,”value”);

 

map = new HashMap();  // error