2014-08-09

Switch Statement

1. Only Integers, char, String and Enum are allowed in Switch statement.

2. If there are no break statements in case blocks, it will execute all statements either till the next break statement or end of switch block

3. Default case will be executed if there is no match, and statements thereafter till break or end of switch

5. Characters and their ASCII value can be used interchangeably

6.      In Case of Enum in the switch statements, Enum options with the Enum prefix or default should be the cases.

Overloading

1. Methods can be overloaded on number of arguments and type of arguments.

2. Static methods can be overloaded.

3. Constructors can be overloaded. this( ) is used to call constructors in chain.

4. Methods cannot be over overloaded on the basis of return type alone.

5. When overloaded on the co variant types, conflict is resolved by the type of reference.

Overridding

1. Overriding Method should have same syntax and return type as Overridden method. If the number and type of arguments varies, it becomes overloading. If the return type is different, compiler gives compilation error i.e "The return type is incompatible with BaseClass.overriddenMethod()".

2. No Explicit cast is required in case of Upcasting i.e derived class reference is assigned to Base class reference but explicit cast is required in case of Downcasting i.e Base Class reference is assigned to Derived class reference.

3. Downcasting may throw ClassCastException if object is found to be non compatible during runtime. That's why instanceof check should be made before doing the downcast.

4. Overridding method cannot throw checked exception wider than the exception thrown by the overriden method. If the overridden method doesn't throw any exception, overriding method can throw any exception.

5. We cannot reduce the visibility of the overridden method. Means If the method in base class is public, we cannot declare it private in derived class.

6. Only instance methods can be overridden. Static methods and instance / static variables cannot be overridden.

7. Final methods cannot be overridden.

8. If a method cannot be inherited, It cannot be overridden. So private methods cannot be overridden.

Serialization

1. Serializable is a marker interface i.e a class need not override any method if it implements serializable interface.

2. A Class should be declared serializable if its serializing or deserializing. In case a class hasobjectOutputStream.readObject() or objectOutputStream.writeObject(), it should be declared serializable.

3. ObjectOutputStream and ObjectInputStream wraps the FileInputStream and FileOutputStreamrespectively.

4. An Object can be serialized only if all of the composed objects are also either serializable or declared transient.

5. If we want any of the instance variable not to be serialized, we should declare it transient.

6. static variables are never serialized.

7. If a parent class is declared serializable , all its derived classes intrinsically becomes serializable.

8. Object Class is not serializable.

9. If you serialize a collection , all its elements should be serializable.

10. Collection Interfaces are not serializable but all concrete classes are.

File I /O

1. File Objects are not used to read or write data. The possible operations that can be performed with File is to make empty new files / directories, searching for files and directories, deleting files and directories and working with paths.

2. FileReader and FileWriter are usually wrapped by BufferedReader, BufferedWriter and PrintWriter for performance and convenience.

3. BufferedWriter provides a newline() method to create platform specific line separator automatically.

4. After Java 1.5, PrintWriter can be build using File and String too along with OutputStream and FileWriter.

5. flush() is used to flush the residual characters in the buffer to the file. It should be the statement before close.

6. file.createNewFile doesn't create the file and returns false if the file with the same name is already there.

7. file.mkdir() is used to create a new directory.

8. File constructor (File,String) can be used to create a file in the sub directory alternatively instead of specifying the complete path as String using (String) constructor.

9.  When there is no more data to read using fileReader.read(), deadline returns null as a mark of read completion.

10. We cannot delete a directory using (File)dir.delete() if its not empty though we can always rename it using dir.renameTo(). To rename it we should specify correct file handle for the new Name as it being null will result in NullPointerException.

11. We can search for files and directories by using (File)dir. list() method and get the list of files and sub directories and then we can loop through the list to find the match.

Collections

1. TreeSet and TreeMap are sorted collections i.e they store the elements in an order.

2. compareTo(Obj obj1) method needs to be implemented if the class implements comparable interface whereas compare(Obj obj1, Obj obj2) needs to be implemented if it implements comparator inyterface.

3. After Java 1.5, TreeSet implements NavigableSet which in turn extends SortedSet. Earlier version used to have TreeSet implementing SortedSet.

4. A Class must override hashCode method if its overridding equal method. Not doing the same may create trouble in with hash collections and hash search.

5. A Class can override hashCode even if it's not overridding equals method.

6. If equals method is not overridden, Two objects are treated equal only if they are same i.e obj1 == obj2.

7. HashCode method should be implemeted such that it should generate same code for an object unless its state is changed. There shouldn't be use of random functions to generate the hashcode.

8. Collection is an interface whereas Collections is a utility class.

9. Set doesn't allow duplicate values.

10. Set can have only one null value but not more than one as duplicates are not allowed.

11. Collections can be of objects only and not of primitives.

12. Arrays.sort only takes arrays as arguments whereas Collections.sort only takes list as argument.TreeSet is used for sorting Set and TreeMap is used for sorting Map.

13. LinkedHashMap maintains the entries in the order in which they were last accessed.

14. HashTable is thread safe whereas HashMap is not. That's why HashMap is faster than HashTable.

15. HashMap can have null keys as well as null values whereas HashTable cannot have either.

16. Sorted collections ( TreeMap , TreeSet ) cannot have heterogeneous objects. Reason being heterogenous object cannot be compared and hence elements cannot be sorted. It throws ArrayStoreException if we try to add non compatible object.

17. Map interface doesn't extend the Collection interface. List, Set and Queue extends Collection interface.

18. If an instance of a class needs to be added to a sorted collection, either the class implements comparable interface or Comparator should be specified during construction of sorted collection.

Var Args

1. Var Args argument should have data type followed by three dots.

2. Three dots should be consecutive and not separated by even space.

3. We can have space before and after the dots. Java ignores the space before dealing with it.

4. If there is a var args in the method, it should be only one and the last one.

5. Internally Java treats var args as array. That is why Java doesn't allow overloaded method to have var args if there is already a method with an array of same type.

String

1. .equals() compares the content of the object whereas == compares to see if the references are holding the same object.

2. Java maintains a String Pool for reusing String objects.

3. String objects are immutable.

4. StringBuffer and StringBuilder object are mutable.

5. StringBuffer is thread safe whereas StringBuilder is not.

6. Literals with double quotes are String Literals.

7. Modifying String objects is not cost effective.

8. Comparing non homogeneous objects using equals always returns false irrespective of their content.

9. .equals method matches the content of String , StringBuffer and StringBuilder because equals method has been overridden in these classes.

10.  String literals gets sorted when added to some collections because it implements comparable interface and hence it defines the compareTo method. StringBuffer does not implement Comparable interface.

11. String is a final class i.e it cannot be subclassed. StringBuffer and StringBuilder are not final classes.

12. String and StringBuilder class implements serializable, comparable and charSequence. StringBuffer only implements serializable.

Generics

Following Generic Declarations are good

List<Exception> li = new ArrayList<Exception>();

List<? extends Exception> li = new ArrayList<FileNotFoundException>();

ArrayList<? extends Exception> li = new ArrayList<FileNotFoundException>();

ArrayList<?> li = new ArrayList<FileNotFoundException>();

ArrayList<?> li = new ArrayList<>();

ArrayList<String> li = new ArrayList<>();

List<? super FileNotFoundException> li = new ArrayList<FileNotFoundException>();

List<? super FileNotFoundException> li = new ArrayList<Exception>();

List<? extends Exception> li = new ArrayList<Exception>();

Following declarations result in compile time error

List<Exception> li = new ArrayList<FileNotFoundException>(); // argument type should be same.

ArrayList<Exception> li = new ArrayList<FileNotFoundException>(); // argument type should be same.

List<Exception> li = new List<Exception>(); // List is not a concrete class but an interface

ArrayList<? extends String> li = new ArrayList<FileNotFoundException>(); // FileNotFoundException doesn't extend String

List<> li = new ArrayList<>(); // incorrect argument

List<? super Exception> li = new ArrayList<FileNotFoundException>(); // FileNotFoundException is not super class of Exception

Show more