Showing posts with label CoreJava. Show all posts
Showing posts with label CoreJava. Show all posts

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