Saturday, December 17, 2011

I/O Stream Classes

  1. Byte Stream
  2. Character Stream
Byte Streams Classes:
  • InputStream (Abstract class)
  • OutputStream (Abstract class)
  • FileInputStream (Reads text from existing file)
  • FileOutputStream (Create a file and write text to it)
  • ByteArrayInputStream
  • ByteArrayOutputStream
  • FilteredInputStream
  • FilterOutputStream
  • BufferedInputStream
  • BufferedOutputStream
  • SequenceInputStream (allows to concatenate multiple InputStream)
  • PrintStream (Used for formatting like "printf" in c)
  • DataInputStream  (Read a sequence of bytes and convert them into values of a primitive type)
  • DataOutputStream (Convert values of primitive type into a byte sequence and then write it to the                 underlying stream)
  • RandomAccessFile 
Character Streams Classes: (unicode characters)
  • Reader (Abstract classes)
  • Writer (Abstract classes)
  • FileReader (Reads text from existing file)
  • FileWriter  (Create a file and write text to it)
  • CharArrayReader
  • CharArrayWriter
  • BufferedReader
  • BufferedWriter
  • PushbackReader
  • PrintWriter (If the argument is not a primitive type, the PrintWriter methods (print, println ,format) will call the object's toString( ) method)

Friday, December 16, 2011

Difference between Iterator and Listitrerator

 Iterator
  • Iterator is used to traverse Set and List and also Map type of Objects.
  • Iterator is used retrieve the elements from Collection Object in forward direction only.
Listiterator
  • List Iterator can be used to traverse for List type Objects, but not for Set type of Objects.
  • List Iterator, which allows you to traverse in two directions.
  • So it has another methods hasPrevious() & previous() other than Iterator.

Difference between ArrayList and Vector

ArrayList 
  • It cannot be synchronized
Vector
  • It can be synchronized

Map Interface

  • Map
  • Map.Entry
  • NavigableMap
  • SortedMap

Map Classes

Map - Have a key and value
  • HashMap
  • LinkedHashMap
  • TreeMap

Collection Classes

Important Collection classes:
List Duplicate objects can be loaded
  • ArrayList implements List
  • LinkedList implements List,Queue,Deque
SetDuplicate objects cannot be loaded
  • HashSet implements Set - (It does not guarantee the order of its elements)
  • LinkedHashSet - (It ensures the order in which it is inserted)
  • TreeSet implements NavigableSet - (Objects are stored in sorted, ascending order)
  • PriorityQueue
  • ArrayDeque - (It can be used as a stack)
Note:
peek() - return and removed

poll() - return but does not removed

Collection framework Interfaces

Collection framework interfaces are:
  • Collection
  • List (Duplicate Objects can be loaded)
  • Queue
  • Dequeue
  • Set (No Duplicate objects)
  • SortedSet (Objects are sorted)
  • Navigable Set

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;
  }

Monday, December 12, 2011

Java.lang.Object

Java.lang.Object Class:
  • For all classes java.lang.Object is a base class.
  • The important methods of java.lang.Object class is:
    • toString();
    • hashCode();
    • equals();
    • getClass();
    • Notify();
    • NotifyAll();
    • Wait();
Note: The methods marked in blue color can override, and the methods marked in red color cannot be override.

Difference between Primitive types and Objects

Primitive Types:
  • Primitives types are loaded in stack.
  • When the primitive is assigned to another primitive type copy is created.
  • Primitive types are not polymorphic, so it cannot be passed as to a method that takes object as a parameter.
Objects:
  • Objects are loaded in heap
  • When object reference is assigned to another object reference,they share the same object
  • Objects are Polymorphic, so they can be passed as a parameter to any method that takes object as parameter.

OOPS Concepts

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
Encapsulation:
  • Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse.
Inheritance:
  • Inheritance is the process by which one object acquire the properties of another object.
Polymorphism:
  • Polymorphism provides a way to take more than one form.
  • Two types of Polymorphism.                        
                               1. Compile Time Polymorphism (Overloading)
                               2. Run Time Polymorphism (Overriding)
Abstraction:

  • Abstraction allows user to view only the essential features without knowing the background details.
  • In other sense we can say that, it deals with the outside view of an object (interface).

Object

  • Object is an instance of a class.
  • Using Object we can access non static member variables and member methods.
  • If we use Objects for accessing static members, a same memory is shared for all Objects.
  • So use class name for accessing static members instead of Objects.

Class

  • Class is a user defined data type.
  • Class contains member variables and member methods.
  • Using Class we can create any number of objects.

Benefits of Java

  • Java is Object Oriented language.
  • Java is platform independent.
  • Java code is reusable and change friendly.
  • Java application don't have memory leak.
  • Java applications are easy to install.

Thursday, December 1, 2011

index

2011
12/11 - 12/18 ==> Basics Theory

2012
01/01 - 01/08 ==> General example Codes
01/15 - 01/22 ==> Collection concept codes