Saturday, March 30, 2013

Difference between C++ and Java

           Java is developed from C and C++. Java is most popular  Object-oriented language.It is a platform independent language. It is used for developing web applications,distributed applications, enterprise applications,etc.

           C++ is an extension of C language.C++ is also object-oriented language but not pure..It is a platform dependent language.It is used for developing desktop applications,drivers,etc.

                 The following are difference between C++ and Java

1. C++ isn't a strictly type-safe language and it supports automatic conversion and truncation. Java is strictly type-safe language and only automatic widening support.

2. C++ has no support for enforced-bound checking whereas java enforces strict bound-check mechanism.

3. C++ support multiple inheritance and java doesn't support multiple inheritance.

4. C++ has a limited set of libraries when compare to java libraries.

5. C++ has a support unsigned arithmetic where as java  doesn't have any such support.

6. C++ support operator overloading but java doesn''t support.

7. C++ requires explicit memory management whereas java has autometic garbage collector for memory management.

8. C++ support call by value and call by reference.But java support only call by value.

9. C++ allow direct call to native  system libraries wheras java uses java native interface to achieve this.

Wednesday, March 27, 2013

Difference between ArrayList and Vector

Difference between ArrayList and Vector                   
                                 ArrayList ,Vector are mot important classes in Java Collections.The most important core java question is Difference between ArrayList and Vector.

1. In Vector, every method is synchronized and thread-safe. But ArrayList doesn't contain synchronized methods and it is not a thread-safe.

                        By default ArrayList is non-synchronized but we can get synchronized version of ArrayList object by using synchronizedList() method of Collections class.

2. Vector the threshold limit, it increase itself by value specified in  capacityIncrement field while you can increase size of ArrayListby calling ensureCapacity() method. 

3.Vector can return enumeration of items it hold by calling elements() method.ArrayList can return listIterator of items it hold by calling listIterator().

4.ArrayList performance is High and Vector performance is low.

5.Vector is introduced in  1.0 version and ArrayList is introduced in 1.2 version.b

6. ArrayList object with default initial capacity is 10 if reaches its capacity a new ArrayList Object is created with 
                           newcapacity=(Currentcapacity*3/2)+1

                Vector object  with default initial capacity is 10 if reaches its capacity then a new Object will be created with 
                      newcapacity= Currentcapacity*2

Tuesday, March 26, 2013

Interface Vs Abstract class

 Difference between Interface and Abstract class                   

                       The tricky and most important question in core java interview is "Difference between Interface and Abstract class".
                       Now let's see difference between Interface and Abstract class in java

1. You cannot create non abstract methods in Interface because every method in iterface by deafault abstract method, but you can create non abstract methods in Abstract class.

2.Interface is suited for Type declaration and abstract class  is suited for code reuse and perspective.

3.When  you add a new method in exiting interface it breaks all its implementation and you need to provide implementation in all clients which is not good,but by using abstract class you can provide deafault implementation in super class.

4.Variable declaration in java interface is by default final.An abstract class may contain non final variables.

5.Methods of interface are public and abstract by default.Methods of abstract class can have  the flavour of class members like private,protected,etc.

6.An interface can extend another interface only but java abstract class can extend another java class and implements more than one interfaces.

7.when compare interface with java abstract class, interface are slow and it requires extra indirection.

8. Interface cannot contain the constructor by default or by programmer but abstract class contains constructor by default or by providing programmer.

Saturday, March 23, 2013


Can Abstract class has constructor in Java?
          Yes,Abstract class has constructor in Java.You will provide constructor explicitly or not,compiler provide default constructor with zero argument  in Abstract class.This is true for all class and also applicable for abstract class.Generally we cannot create object for abstract class by using new operator.
         We can create child class by extending abstract class.When we creating object for child class, child class constructor calls parent class constructor i.e,Abstract class constructor but not creating object for abstract class.

Friday, March 22, 2013

Is possible to print a statement on console without using Main() method in Java?

                        Yes it is possible to write java program without using main() method upto 1.6 version.From 1.7 version onward main() is compulsary.  In java we have the concept static block.static block is executed at the time class loading. So JVM executes static block without object.
                 For this to follow below program
 class Example 
{
   static
    {
       System.out.println("I am executing java program without 
                           using main() method");
     }
}

Output:
            I am executing java program without using main() method