Interface vs abstract class vs concrete class

The following points make you clear when to use interface, abstract class and concrete class in your java code implementation for a requirement.
  1. When there is only requirement specification provided and there is no clarity on the implementation, then we need to go for an interface to get the high level view of the requirement. This is 100% abstraction. Example: Servlet
  2. When we know about the implementation partially, then we should go for abstract classes. Abstract classes in this level help the programmer to add the implementation in future. Example:Generic Servlet, HTTPServlet
  3. If we know the implementation completely and ready to provide the service with the APIs, then we can go for concrete classes. Example: MyOwnServlet.
At the end of the requirement, we only need concrete classes to expose the API and to provide the services. The Interfaces and the Abstract Classes are used to develop the concrete classes.

String vs StringBuffer vs StringBuilder

The most important question in interviews is when to use String, StringBuffer and StringBuilder.
  1. If the content is fixed and do not change frequently , then we need to use String objects.
  2. If the content is not fixed but thread safety is required, the we need to use StringBuffer Objects.
  3. If the content is not fixed and thread safety is not required, then we can use StringBuilder objects.

Refer:

What is the difference between StringBuffer and StringBuilder?
What is the difference between String and String Buffer?

What is the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder both are used for mutability. Both have same methods and constructors but all the methods in StringBuffer are Synchronized whereas, the methods in StringBuilder are not Synchronized.
As the methods in the StringBuffer are synchronized, only one thread can operate on stringbuffer object at a time which gives thread safety, but this reduce performance.Hence wherever performance is more concerned than thread safety, we have to use StringBuilder over StringBuffer.

The difference between StringBuffer and StringBuilder are shown below:
StringBuffer and StringBuilder Differences

what are the various modifiers available in java?

The access modifiers place a vital role in java programming.
The list of various modifiers available in java is as follows:
  1. public
  2. private
  3. protected
  4. <default>
  5. abstract
  6. final
  7. static
  8. native
  9. synchronized
  10. strictfp
  11. transient
  12. volatile
In java there is no such thing called access specifiers and all these are considered as access modifiers.
The modifiers and there applicability on classes, methods, blocks, variables, interfaces, constructors and enums are given below.

Modifiers and their applicability

Important Conclusions:

  1. The modifiers wihich are applicable for inner classes but not outer classes are private, protected, static.
  2. The modifier that is applicable to class but not interface is final
  3. The modifiers that are applicable to classes but not enums are final and abstract.
  4. The modifier which is applicable to only methods and cannot be used anywhere else is native.
  5. The only modifiers which are applicable to constructors are public,private,protected, default.
  6. The only applicable modifier for local variables is final.
We can declare,
  • a class inside a class
  • a class inside an interface
  • an interface inside an interface 
  • an interface inside a class.
The interface which is declared inside a class is always static implicitly.
The interface which is declared inside an interface is always public and static implicitly.
The class which is declared inside an interface is always public and static implicitly.

Illegal Combinations :

 Methods:
  1. If a method is public, we cannot declare it as private or protected.
  2. If a method is abstract, then we cannot declare it as final, static, private, synchronized, strictfp, native.
Variables:
  1. If a variable is public, we cannot declare it as private or protected.
  2. If a variable is final, we cannot declare it as volatile.
Class:
  1. If a class is public, we cannot declare as private or protected. 
  2. If a class is final, we cannot declare as abstract.
This is applicable to inner classes also .
 

what is the difference between ''=='' operator and equals() method in java?

In general, we use == operator to compare references or addresses and equals() method is used to compare contents.

example: 

String s1 = new String("java");
String s2 = new String("java"); 
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

O/P:  
false
true
Here, the == operator compares  the addresses of s1 and s2 which are clearly pointing to different address locations and hence the result of this comparison is false. Whereas, the equals() method compares the contents of s1 and s2 and hence result is true.
 

What is the difference between String and String Buffer?

String objects are "immutable" and String buffer objects are "mutable".

String:

Once we create a string object, we cannot perform any changes on that same string object. If we try to perform any operation on that object, then a new object will get created with those changes. This non changeable behavior of string object is called "immutability". 
example:
String s = new String("Programmers"); // a string object s is created and has a value "programmers". 
s.concat("language"); // new object "Programmers language" will be created and left unassigned to any variable.(will be removed by garbage collector) 
System.out.println(s);   
o/p: Programmers

String Buffer:

Once we creates a string buffer object, we can perform any type of changes on the existing object itself. This changeable behavior of String Buffer objects is called "mutability".
example:
String sb = new String("Programmers"); // a string object s is created and has a value "programmers". 
sb.append("language"); // object  will be modified to "Programmers language"
System.out.println(sb);   
o/p: Programmers language

What is the difference between final, finally and finalize?

The similarity in the names may be one of the reason to ask this question in many number of interviews.

final:  

final is a modifier applicable for classes, methods and variable.
  • If we declare a variable as a final, it will become constant and we cannot perform reassignment to that variable. 
  • If a method is declared final in a class, the method cannot be overridden in the child classes.
  • If a class is declared as final, then the class cannot be extended ie., we cannot create a child class to that class.

finally:

finally is a block always associated with try / catch block to maintain cleanup code.
try
{
   // Risky code
}
catch(Exception e)
{
    //handling part of code
}
finally
{
    //cleanup code(like resource de-allocation)
}

 finalize:

finalize() is a method present in "object" class. finalize is called by garbage collector to perform cleanup activities before destroying an object.

Note:

finally block is meant for cleanup activities related to the try block. The resources which are opened in the try block code are closed in the finally block. Whereas, finalize() method is related to cleanup the resources which are related to the unused objects and is related to garbage collector.