Tuesday, December 13, 2011

Marker Interfaces

  • Interfaces with no methods are known as marker interfaces.
  • Marker interfaces are Serializable, Clonable, SingleThreadModel, Event listener.
  • Marker interfaces are also called "tag" interfaces.
  • All null interfaces are marker interfaces but all markers are not necessary to be null interfaces.
Uses:
  • I read "it tells the compiler that it should be treated differently ". 
  • "It is used to store state of an object". what is mean by "storing state of an object.


Note: A class implementing null interface, the behavior of object will not change.

Abstract Class

  • Abstract classes are used to declare common characteristics of subclasses.
  • An abstract class can have instance methods that implement a default behavior.
  • An Abstract class does not require abstract methods.
  • An  abstract class cannot be instantiated.
Uses:
  • It is used for Sharing a common algorithm accross multiple classes.


Interface

  • Interface is a 100% abstraction.
  • Interface can only declare constants and instance methods.
  • All member variables in interface are final and static by default.
  • All the methods in an interface are "public and abstract" by default.
  • Interface cannot be instantiated.
  • Interface without methods is called null interface.
Note: Abstraction - Show only the essential features without knowing the background details.

Exception Handling

  • Exception Handling is used to handle run time errors.
  • There are three block in exception handling mechanism.
                            1.try
                            2.catch
                            3.finally

try block:
  • The code which may raise exceptions should be written inside the try block.
catch block:
  • If the error is raised in try block, the catch block will catch that error and handle that error.
  • If the try block will not raise any exceptions, catch block will not be executed.
finally block:
  • The try block is used to clean up the resources, what we have opened for operations in the try block. (The resources may be files or database connections, etc..)
  • The try block will always executed if the try block exist, except the System.exit(0) call.
  • The finally block will executed, even if the error is not raised in try block.
Important Methods of Throwable class are:
  • getMessage(); //gives the error description
  • printStackTrace(); // gives the error path
  • getCause(); // cause is nested error object ==> Throwable t=e.getCause(); t1.printStackTrace();
Note: Throwable class is the base class for Exception class.

Code Inlining

  • Assigning value to a variable at compile time is known as Code Inlining.
                       Eg:  int a=10;
                              int b=20;
                              int c=a+b;

Finalizer

  • Finalize is a special method that will be called only by a garbage collector before removing the objects from the heap.
  • When the object is set to null, finalize is not called.

Final Keyword

Final Keyword used in 3 places:
  1. It is used to define a constant.
  2. It is used to prevent method overriding.
  3. It is used to prevent Inheritance.

super Keyword

super Keyword is used in 2 places:
  1. super is used to call the overridden method from child class to base class.
  2. super is used to pass the parameter from child class constructor to base class constructor.

Static Constructor

  • Static constructor gets only once, even through any no. of objects are created.
  • Static constructor will be executed even if we access the members of a class instead of creating objects (But it will be executed only once).

Static

  • Static means common to all.
  • The static members and static methods can be called without creating objects.
  • If static members and static methods are called through objects, it shares the same memory for all objects.
  • Static is used for sharing.

Constructor

  • Constructor is a special method called by the run time for every object while instantiating the object.
  • Objects cannot be created with out Constructor.
  • If a class does not have any constructor, then a java compiler will add parameter less constructor to the class. This constructor is called default constructor.
  • If the parameterised constructor is added to a class, then a compiler will not add default constructor to the class.

Memory Management:

  • Java provides automatic Garbage Collection.
  • Garbage collector will be executed automatically when the heap is almost full.
  • On demand we can run the Garbage Collector by calling gc() method.
                              Code:   Runtime.getRuntime().gc();

Java Interview Questions Link

Wrapper Class

  • Wrapper Class is used to convert primitive type to Objects.

Difference between StringBuffer and StringBuilder

StringBuffer:
  • StringBuffer is used in used in Multi user environment.
  • It can be Synchronized.
  • It is Thread safe.

String Builder:
  • StringBuilder used in Single user environment.
  • It cannot be Synchronized.
  • It is not Thread safe.

String Buffer

StringBuffer:
  • StringBuffer is alternate to String.
  • StringBuffer is not immutable.(It can be altered)
  • The default capacity of String Buffer is 16 characters.
  • String Buffer is Synchronized(Thread safe).
Note: To see the contents of String Buffer, type "javap java.lang.StringBuffer" in cmd.

String

  • String is immutable.(once a value is assigned to a string object, it cannot be changed).
  • String can be instantiated through Constructor or explicitly.
  • If the String is instantiated explicitly, it is loaded in Intern pool (Special memory in heap).
    • Eg:  String str1="Hello";   String str2="Hello"; //No duplications (Loads in InternPool)
    • When multiple String variables are assigned a same string value explicitly, all variables will share one string object loaded in the Intern Pool.
  • If the String is instantiated through Constructor or any method of string will be loaded in the Heap (not in Intern Pool) 
    • Eg:  String str1=new String("Hello"); //Have duplications (Loads in Heap)
Note: To see all the contents of String class, type "javap java.lang.String" in cmd.

this Keyword

  • this refers to the current object
  • this keyword helps us to avoid name conflicts.
Eg:
  int length,breadth;
  void show(int length,int breadth)
  {
   this.length=length;
   this.breadth=breadth;
  }