what is the difference between ArrayList and DoubleLinkedList?


ArrayList
LinkedList
ArrayList is implemented as a resizable array
LinkedList is implemented as a Double LinkedList
As more elements added to ArrayList, its size is increased dylnamically
LinkedList performance on add & remove is better than ArrayList
ArrayList elements can be accessed directly by calling get() and set() methods
LinkedList worse on get() and set()methods

When to use LinkedLIst:
  1. Your application can live with out random access because if you need nth element in LinkedList, you need to traverse upto nth element and then you can get data from that node.
  2.  If your application have more insertion and deletion operations more than retrieval. 
  3.  LinkedList is much faster than ArrayList since removal does not involve resize.

What is the difference between ArrayList and Vector?

ArrayList
Vector
It is not synchronized
Vector is synchronized
In case of single thread, ArrayList is faster than Vector
In case of multiple threads, Vector is advisable, with single thread model Vector becomes slow
ArrayList increases it’s size by half every time
Vector increase it’s size double

core concepts in java

What is a class?
  • Class is an user defined data type
  • Class is nothing but blue print of an object i.e common specification of an object
  • Class contains data and member functions
What is an object?
Object is physical representation of a class
  • Object is a real time entity
  • Object is an instance of a class
What is Encapsulation?
  • The process of hiding internal data from the external world and accessing it only through publicly exposed methods is known as encapsulation.
                            OR
  • Wrapping of data and member functions into a single unit so that we can provide security for the data

Example:
 bank.java
class bank
{
      private double balance;  //by declaring balance variable as PRIVATE, we can encapsulate
      public void  setBalance(double balance)
      {
            this.balance=this.balance+balance;
       }
      public double getBalance()
      {
              return balance;
       }
}

customer.java

class customer
{
     public static void main(String a[])
    {
              bank b1=new bank();
            //b1.balance=4000; compile time error ll occur, bcoz balance variable ENACAPSULTED by declaring as PRIVATTE
              b1.setBalance(4000) ;
             //system.out.println(b1.balance); compile time error
              System.out.println(b1.getBalance());
     }
}
What is abstraction?
The process of hiding essential details is known as abstraction.
Abstraction has 2 types, they are
1.      Data abstraction:
                        We know that there will be some kind of data and we can express
              Eg: vehicle engine no, chasis no
2.      Functional abstraction:
          We can feel the behavior of an action but we cannot express what exact process going inside the method.
Eg: Applying acceleration and breaks to the vehicle.
What is polymorphism?
Polymorphism is the process of “an object can exhibit different type of  behavior at different instances.”
This polymorphism has 2 types.
1.      Function overloading :
The process of defining multiple methods “with same name and different signatures(parameter type/list)”                                    Or
A class can have more than one function “with same name and different signatures(parameter type/list)”

2.      Function overriding:
The process “redefining super class methods into sub classes” called function overriding
                                                            Or
A class can have more than one function “with same name and same signatures(parameter type/list)”.

What is Runtime polymorphism or Dynamic binding?
The process of  “calling of a code without knowing it until the runtime” is called runtime polymorphism or dynamic binding.

What is Message passing?
The process of “communicating one object with another object” is called message passing

What is inheritance?
  1.  The process of “obtaining one object properties into another object” is called inheritance.
  2. It supports reusability.
  3. We can create objects only for child classes.
                                    Or
                      Inheritance is the process of “acquiring the properties of super/base/parent class into sub/derived/child class
Example:
class parent                                                                    
{
            int x;
public void readx(int a)
{
X=a;
            }
}

class child extends parent
{
            int y;
            public void ready(int b)
{
            Y=b;
            }
public int getSum()
            {
                        Return x+y;
            }
            public static void main(Sting a[])
{
            child c=new child();
            c.readx(7);
            c.ready(9);
int sum=c.getSum();
                        System.out.println(“sum is”+sum);
            }
}

What is Type casting?
The process of "converting one data type into an another data type" is called type casting.
Type casting have 2 types, they are
  1. Implicit type casting
  2. Explicit type casting
Example:
                   int x=10;
                   float f=7.6;
                   d=x; // implicit type casting
                   x=int(f);   //explicit type casting

What is Type conversion?
  • Type conversion is the process of primitive types into their respective data type value
  • In this we use wrapper class

 

What is an Autoboxing?

The process of "implicit conversion of object type into primitive type" is called autoboxing.
What is wrapper class in java
  • "For every standard data type there will be a corresponding class" called wrapper class.
  • These wrapper classes can be used in type conversion.

What is an instantiation in java?

Instantiation is the process of "allocating memory for an object i.e. memory will be allocated for variables under that particular object."

What is initialization in java?

The process of "initializing the object i.e. values will be assigned to the variables under that particular object."
What is Constructor in java?
  • Constructor is a method .
  • Constructors are used to initialize the object.
  • Constructors are called when object is created.
  • Constructor name and class name should be the same.
  • Constructors are return a value of its same type.
  • Constructor should not have any return type.
What is String handling in java?
  • String is an immutable that means we can define string but we can not change.
                            String s1="java smart answers";
                                                                              
                            String s2="java smart answers";

  • If two or more string object contents are equal then there will be only one object. From the above, both s1 and s2 referring only one object
  • In case of "==" , we will check the reference or hash code of the object.
  • In case of equals() method, it will check the content of object, it will returns a boolean value i.e true or false.

Why String is an immuatable?

String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

For example:-
We have an employee named as Pawan, company HR department decided to give the bonus for incredible performance in the project development while firm team in crucial period.

So, for that I need to add bonus to his salary for encouraging him, now if String is not immutable then that bonus will be added to the so many employees of whose named as Pawan, because lot of people exist in a firm/company/organization with same name.

In order to avoid the above problem String is set was an immutable, if it is not immutable the company should lose huge money in term of crores.

What is the Difference between final, finally and finlize() method

Final and finally is keywords where as finalize() is a method


Finalize() is a method, used to call garbage collector to reclaim the memory.

Finally:
Irrespective of the exception raised, finally block will be executed.

Final:
In java, final is a keyword used to define constants.
Final keyword can be applied in 3 levels mainly.
1.   Class level
2.   Method level
3.   Variable level

Class level:
Final class:
The class declared as final can't be subclass or extend the class

The final class declared as follows :
public final class MyFinalClass

Method level:
final method:
when we declare a method as final, Then final method can't be override in a subclass.
     The final method  can be declare as follows:
public final String convertCurrency()

Variable level
Final variables:
When we declare a variable as final, it behaves like a constant.
      Means once it is declared, it can't be changed.
      Attempt to change  it's value lead to exception or compile time error.
You can declare the final fields as :

public final double radius = 126.45;

What is an abstract class in java, and when we should it be used?

Class(don't turn without reading important note given below)

Abstract classes are classes that contain one or more abstract methods.Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

Method

An abstract method is a method that is declared, but contains no implementation means the child which is going to inherit from the parent should provide implementation.

Let's look at an example of an abstract class, and an abstract method.

For example[optional means for understand sake]

Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking). 

public abstract Animal
{
   public void eat(Food food)
   {
        // do something with food....
   }

   public void sleep(int hours)
   {
        try
            {
                        // 1000 milliseconds * 60 seconds * 60 minutes * hours
                        Thread.sleep ( 1000 * 60 * 60 * hours);
            }
            catch (InterruptedException ie) { /* ignore */ }
   }

   public abstract void makeNoise();
}

Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.

public Dog extends Animal
{
   public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
   public void makeNoise() { System.out.println ("Moo! Moo!"); }
}


Very important note:

Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.

Why should we use Interface in java?
By default all variables are static final variables
By default all methods are abstract methods

Interface having only abstract methods

The class which is going to inherit from the interface that class should provide implementation to all abstract methods.
Usage:

There are mainly three reasons to use interface. They are given below.

It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.

What is the difference  between Abstract class and Interface in Java?

Absract Class
Interface
When to use ? :
At the time of impementing the Inheritance if you found few common and few specific state or methods or behavior in both parent and child class, You should use abstract parent class and your parent abtract class should contain:
  1. Common methods (non-abstract) with non-private visibility so child class can access them.
  2. Abstract methods which child class are suppose to implement as per requirement.
  3. You can also put common state or behavior of Inheritance hierarchy classes here. 
When to use ? :
At the time of implementing the Inheritance when there is no any common state or method or behavior and you want certain kind of contract to be implemented by child classes by overriding the methods you should use Interfaces. Your Interface should contain:
  1. Only abstract methods (behavior only - no state).
  2. Global constants.
Inheritance implementation with Abstract or Concrete class is called Generalization.
Inheritance implementation with Interface is called Realization.
You can write both abstract and non-abstract methods in abstract class.
You can write only abstract methods in Interface as It represents a kind of Contract with Concrete class needs to implement.
You can have static and non-static variables / state or methods / behaviros in abstract class.
You can have only non-static methods / behaviros and Global constants (public static final variables).
Generalization.
Realization.
Concrete or Child class can not extend any other class because it has already been extended from Abstract parent class. But It can still implement any other Interfaces if required.
Concrete or Child class can extend any other one class if necessary and It can also implement any other Interfaces if required.
  • You can have Bank as Abstract parent class and HSBCBank, AmericanBank etc. as concrete classes.  
  • Put common methods or behaviors of banks in Bank class and specific behaviors in concrete classes.
  • You can define RuleEngine as Interface and SalesRuleEngine, FinanceRuleEngine as concrete classes in your application.
  • SalesRuleEngine and FinanceRuleEngine are following the contract defined in RuleEngine interface.

Example:
abstract class Bank {
    // common methods
    public int withdraw() {
        // method body
    }
    public boolean deposit() {
        // method body
    }
  
    // abstract methods
    public abstract void openAccount();
    public abstract void closeAccount();
}
class HSBCBank extends Bank {
    public void openAccount() {
        // method body
    }
    public void closeAccount() {
        // method body
    }
}

Example:
interface RuleEngine {
    // by default variables are public static final only
    int RULE_CONSTANT = 1;
  
    // contract methods needs to be implemented by concrete classes.
    // by default declared methods are public abstract only.
    void createRule();
    void updateRule();
    void deleteRule();
    public abstract void getAllRule();
}
class SalesRuleEngine implements RuleEngine {
    public void createRule() {
        // method body
    }
    public updateRule() {
        // method body
    }
    public void deleteRule() {
            // method body
    }
    public void getAllRule() {
        // method body
    }
}
Some important abstract classes in Java API are:
  1. AbstractList
  2. AbstractSet
  3. AbstractMap etc...
Some important Interfaces in Java API are:
  1. Serializable
  2. Clonnable
  3. List, Set, Map etc...