Monday, August 11, 2014

Servlet and Database



Servlet Lifecycle Init(config) Service(request, response) Destroy()

Servlet Communication RequestDispatcher interface SendRedirect() method

JSP JSP is a servlet that calls RequestDispatcher Interface

JSP Lifecycle Init() -> service() -> destroy()

Cookie VS HTTP session Cookie is a user state storing in client side whereas HTTP session is on server side.

Different Scopes Page Request Session Application

EL in JSP Expression language used to provide the object and values.

Hibernate has thread safe session factory

Hibernate get() vs load() Returns real object – proxy object returned here. Null pointer handled – cannot point to null object

Hibernate update() vs merge() In session – out of session

Hibernate object states Transient – not associated with session, just values. Persistent – in session with database. Detached – session closed / disconnected.

Hibernate first level cache VS second level cache Session – session factory Enabled – not enabled by default

SQL cursor Temporary work area in system memorywhere SQL is executed

Java Basics



List VS Set List has duplicate – No duplicates allowed in set

HashMap VS HashTable HashMap is not threadsafe and not synchronized - HashTable is threadsafe and syncronized HashMap is fast - HashTable is not as fast as HashMap Concurrent HashMap is thread safe

HashMap VS HashSet HashMap can allow duplicate values - HashSet do not allow duplicates HashMap is Fast - HashSet is Slow HaspMap has Key - Value Pair - HashSet has values alone

Comparator VS Comparable Comparator is used for Object attribute comparison - Comparable is used for object comparison Compare() - CompareTo()

Array List VS Vector Array List is not synchronized – Vector is synchronized Array List is not a legacy class – Vector is a legacy class Array List increases by 50% of its size – Vector increases by doubling its size

Serialization To ensure that the same object is transferred over the network we use serailization Same serial UID should be used in server and client else u get invalid class error

3 JDBC Statements Statement – query compiled each time PreparedStatement – query compiled only once CallableStatement – execute stored procedures and functions

Transaction Management Connection interface provides commit(), rollback() etc.

getHashCode() Used to get unique code to store in Hashmap

Core Java Concepts



JVM : JVM is an abstract machine where java byte codes can be implemented.

JRE : It is an implementation of JVM and physically exists.

JDK : JRE + Development Tools

JIT : Instruction set of JVM to Instruction set of CPU. It compiles similar code at the same time, so faster than other compilers.

Object based programming language: JavaScript and VBScript are object based programming language.

Static variable: It provides only one instance of the variable per JVM.

Static Method: It can be invoked without creating an object to the class and can access static variables.

Main method is static A main method is static because it needs to be called without an object.

Substitute for main method We can use a static block like, Class A{ Static {}}.

Final Final variable – constant variable value Final method – Cannot be overridden Final class – Cannot be inherited

Abstract Class: It is a class that needs to be extended and cannot be implemented.

Abstract and final Abstract and final cannot exist together.

Interface Interface has method definitions and provides abstraction and multiple inheritances.

Interface cannot be static and final

Abstract VS Interface Method body can be present - only abstract methods. Variables - no variables Can have constructor – No Static methods – no You can extend abstract classes – you can implement multiple interfaces.

Composition Using another class inside the class without inheriting it is composition.

Virtual Functions All functions in java are virtual functions.

Immutable Once value is assigned it cannot be changed. String is immutable.

Immutable Once value is assigned it cannot be changed. String is immutable.

Reflection It is modifying runtime behavior of a class at runtime. EG: Debugger, Eclipse, Testing tools.

Wrapper Classes They allow primitive times to be accessed as objects.

Singleton Class It has one instance per JVM only.

Base class for error and exception Throwable.

Effective Java



Static factory methods: No need of using multiple overloaded constructors. No need of memory allocation each time a new object is created. Object also can be returned. It encourages composition instead of inheritance.

Memory Leaks in Java: Assigning values to an array and leaving the array like that in memory even after array processing completes causes memory leaks. It can be avoided by nullifying the array. Use finalizes to remove the objects from memory. (object.terminate()) Exception:“OutOfMemory” exception.

Using classes in Java: We need to minimize the accessibility of classes and members. (Use private final as much as you can) Use composition in place of inheritance.

Immutable Objects: String, Integer etc… The states of these objects cannot change after construction. They are threading safe and exception on them is handled.

Composition instead of Inheritance: Just implement the desired functionality instead of inheriting the whole class.

Tuesday, August 5, 2014

Core Java Hints



Java is Object Oriented Programming : Objects are the units of the program.

Object = data + code Data access within the object only.

Objects: It is the instance of a class. When an object is created using new memory is allocated to it.

Encapsulation: It is the mechanism that binds code and data together and keeps it safe from outside interference. Class will have total control of what it stores.

Inheritance: It is the process by which one object acquires the properties of another object. Interface can be implemented by multiple class files.

Polymorphism: If we have the same function in the base class and extended class, we would display the function from the extended class. We can access the base class function by using “super.function”.

Primitive Data Types: Byte, short, int, long, float, double, Boolean, char

Access Modifiers Default – Can be accessed within the folder only. Public – Can be accessed any were. Private – Can be accessed only inside the class. Protected – Can be accessed within the folder and subclasses outside the folder.

Constructor VS Method Constructor will be automatically called when we call the object, whereas a method should be called explicitly.

Uses of Bin and Lib folders in JDK Bin contains JAVAC (Compiler), AWT tool, applet viewer etc., whereas Lib contains API and other packages.

final, finalize () and finally A final class cannot be overridden or extended to subclasses. Finalize function will be called just before destruction of a class. Finally is used in exception handling where it will be executed whether or not exception occurs.

Garbage Collection We can call garbage collection explicitly by using “System.GC()”.

Transient VS Volatile Transient variables can be in a class but do not participate in persistence whereas Volatile variable can change its value unexpectedly by other processes.

Overloading VS Overriding In Overloading we have two functions with the same name but different parameters whereas in Overriding we have sub-class function replace the super-class function of the same name.

Inner Class VS Anonymous Class Inner class is defined inside a class whereas Anonymous class is declared without a name and instantiated and declared in the same class and cannot have explicit constructors. And is described here, new interface-or-class-name() { class-body } runnable r = new runnable() { public void run() { //code for the run method goes here } };

Integer VS Int “Int” is a primitive data type for calculations whereas “Integer” is from “Java.lang” package and used in methods.

Abstract Class VS Interface - Any variable declaration. - Only Public static final variables and public methods allowed. - Related 1 class only implement abstract. – Unrelated classes can implement an interface.

Inner thread communication functions Wait(), notify(), notifyAll()

Applet lifecycle Init() -> Start() -> paint() -> stop() -> destroy()

AWT VS SWT VS Swing VS GWT AWT, SWT and Swing are Graphical UI tool kits for java. GWT JavaScript based UI application development and maintenance tool.

Vector VS Hash table VS Linked list VS Enumeration Vector is a collection of objects. Hash table stores index and objects using hash function. Linked list involves sequential storing and retrieval of data. Enumeration stores and gives one element at a time.

Set VS List Set stores in unordered way and cannot have duplicates and it’s the opposite for Lists.

Types of statements in JDBC createStatement() preparedStatement() CallableStatement()

Servlet Lifecycle Init() -> service() -> destroy()

Cookie VS Session Tracking Cookie is storing the state information of a user session by using a file in client side whereas tracking is done in server side.