Thursday, May 27, 2010

When to use Interface and abstract class

Interfaces are contracts between groups of programmers who write code without any knowledge of what other programmers are writing. To keep them (Programmers) know what other programmers are doing, interfaces are the way in java.

Abstract classes are same as interfaces, with few differences. They can contain implemented method bodies, fields which are not static and final. They are useful when some methods are common for classes and some method implementations are different. So, we can implement common methods in abstract class and write method (which are not implemented) signatures in it. What ever the class is implementing abstract class will provide their own method implementations for methods which are not implemented in abstract class.

Look at this tutorial if you've more doubts on this topic.

Wednesday, May 26, 2010

Need for Just In Time compiler


Basic function of just in time compiler is to transform bytecode into machine dependent code. Instead of interpreting bytecod line by line, it converts bytecode into machine dependent code very fast (It tries to identify which instructions will be executed next, so that it can execute code in advance). It's used to increase the performance. One can see the performance difference if the same method is called more than one time.

Difference between Synchronized method and Synchronized block

Why we need synchronization in multithreading?

Because multithreading introduces asynchronous behavior to your programs, there must be a way to enforce synchronicity when you need it. For example, when you want two threads to communicate and share a complicated data structure, you need some way to ensure that they never conflict with each other.

One way to synchronize object methods is using synchronized methods as shown below

synchronized void call(String message)
{
System.out.println(message);
}

But, it'll not work in all cases. For example, if you want to synchronize access to objects of a class that was not designed for multithreading or what if the class was not created by you, but by some third party and you don't have access to the code? Solution is that you simply put calls to the methods defined by this class inside a synchronized block as shown below

Synchronized(Object)
{
statements to be synchronized
}

Here Object is a reference to the object being synchronized. Synchronized block ensures that a call to a method that is member of this object occurs only after the current thread has successfully entered object's monitor. (Monitor is an exclusive lock for the object being synchronized)

Tuesday, May 25, 2010

Difference between final, finally and finalize

Final is used to declare constants as shown in below example.

int final a = 10;

Class can also be a final class, which shows that the class can't be subclassed.

Method can also be final method, which shows that the method can't be overridden.

Finally is used to release any resources used in try catch block to free memory. It is placed after try-catch block. Code in finally block is executed always irrespective of the exception thrown except when System.exit(0) is called. It is shown in the below example

try
{
System.out.println("Enter your name:");
String name = scan.next();
System.out.println(name);
}
catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
finally
{
scan.close();
System.out.println("Closed");
}
Code in finally block is get executed. If you put System.exit(0) inside try block or inside catch block, then finally block is not called.

finalize() method is called before an object is collected by garbage collector to free any resources being used by that object

Difference between String and String Buffer in java

String is a immutable object, where StringBuffer is a mutable object in java. Still you'll be able to change string using '+' operator. But every time you add some thing to string object using '+', a new string object is created, So, more memory is need to hold those new objects. If you just use concat method of string object, it'll not concatenate the new string as shown in below example.

String abc = "Hello ";
abc.concat("changed");
System.out.println(abc);

Most of the people think that output of the above example is 'Hello changed' (with out single quotes). That's correct, but new object has been created with value 'Hello changed' (Without single quotes) and the previous value is recommended for garbage collection.

String abc = "Hello ";
abc += "changed";
System.out.println(abc);

Output of the above code is 'Hello changed' (without single quotes). But, a new string object is created to hold the string "Hello changed".

If you use StringBuffer instead of String in the above two examples you'll save memory(example 2) and you'll get the output what you intended for (example 1) as shown in the below example

StringBuffer abc = new StringBuffer("Hello ");
abc.append("changed");
System.out.println(abc);

Output: Hello changed

Thursday, May 20, 2010

How to access method variable in a innser class declared inside method

Variables declared inside a method can be used inside the inner class declared inside this method. But, those variables must be declared as final variables as shown in the below example.


public class InnerClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InnerClass ic = new InnerClass();
ic.inner();
}
public void inner()
{
final int a = 10;
class Inner
{
public Inner()
{
System.out.println(a);
}
}
new Inner();
}
}

Output: 10

How to call Constructor from Constructor in the same class

You can call one constructor from another constructor if the class has more than one constructor in java. Here is the example. Note that constructor call must be the first statement in a constructor

public class Constructor {
int a;
int b;
Constructor()
{
this(1, 2);//This must be the first call.
System.out.println("hello");
}
Constructor(int a, int b)
{
System.out.println(a+", "+b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Constructor();
}
}

Output:
1, 2
hello