Posts

How SQL Indexes Work internally ?

Image
When an index is created, it records the location of values in a table that are associated with the column that is indexed. Entries are added to the index when new data is added to the table. When a query is executed against the database and a condition is specified on a column in the WHERE clause that is indexed, the index is first searched for the values specified in the WHERE clause. If the value is found in the index, the index returns the exact location of the searched data in the table. Figure 16.1 illustrates how an index functions. Suppose the following query was issued: SELECT *  FROM TABLE_NAME WHERE NAME = 'SMITH'; As shown in Figure 16.1, the NAME index is referenced to resolve the location of all names equal to 'SMITH'. After the location is determined, the data can be quickly retrieved from the table. The data, in this case names, is alphabetized in the index. A full table scan would occur if there were no index on the table and the same que...

Java Documentation Tips and Tricks Referral link

A great source to know about Java Documentation. http://zeroturnaround.com/rebellabs/reasons-tips-and-tricks-for-better-java-documentation/?utm_source=mkto&utm_medium=email&utm_campaign=RebelLabsNewsletterApril

How does java Hashmap works internally,,!

What is Hashing? Hashing in its simplest form, is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. A true Hashing function must follow this rule: Hash function should return the same hash code each and every time, when function is applied on same or equal objects. In other words, two equal objects must produce same hash code consistently. Note : All objects in java inherit a default implementation of hashCode() function defined in Object class. This function produce hash code by typically converting the internal address of the object into an integer, thus producing different hash codes for all different objects. HashMap is an array of Entry objects: Consider HashMap as just an array of objects. Have a look what this Object is: static class Entry<K,V> implements Map.Entry<K,V> {           final K key;           V value;        ...

Mutable and Immutable Objects

Mutable Objects:  When you have a reference to an instance of an object, the contents of that instance  can  be altered Immutable Objects:  When you have a reference to an instance of an object, the contents of that instance  cannot  be altered Immutability and Instances To demonstrate this behaviour, we'll use  java.lang.String  as the immutable class and  java.awt.Point  as the mutable class. Point myPoint = new Point( 0, 0 ); System.out.println( myPoint ); myPoint.setLocation( 1.0, 0.0 ); System.out.println( myPoint ); String myString = new String( "old String" ); System.out.println( myString ); myString.replaceAll( "old", "new" ); System.out.println( myString ); In case you can't see what the output is, here it is: java.awt.Point[0.0, 0.0] java.awt.Point[1.0, 0.0] old String old String We are only looking at a single instance of each object, but we can see that the contents of  myPoint  has change...

When to go for Interfaces and Abstract Classes?

From Java How to Program about abstract classes: Because they’re used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses. These classes cannot be used to instantiate objects, because abstract classes are incomplete. Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects. Otherwise, these subclasses, too, will be abstract. To answer your question "What is the reason to use interfacs?": An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design. As opposed to an interface: An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for all the methods... Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality...

Difference between Comparable and Comparator?

Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface. Comparator provides a way for you to provide custom comparison logic for types that you have no control over. Comparable allows you to specify how objects that you are implementing get compared. Obviously, if you don't have control over a class (or you want to provide multiple ways to compare objects that you do have control over) then use Comparator. Otherwise you can use Comparable. Other Sources: http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html http://www.programcreek.com/2011/12/examples-to-demonstrate-comparable-vs-...

What are Interfaces and When to use Interfaces?

Interfaces: A good place to look at Java Interfaces would be the collections framework. java.util.List //interface java.util.ArrayList //Concrete class java.util.LinkedList //Concrete class So you can write code like this: List l = new ArrayList(); l.add(..) //do something else. If in future you want to change the implementation with say LinkedList or you own AwesomeList which implements List interface, all you have to do is change the very first line to: List l = new MyAwesomeList(); or List l = new LinkedList(); The rest of the code would follow through. More Examples: Use interfaces to define an application programming contract (blueprint, interface) which "3rd-party" vendors have to fully adhere and implement. This way the endusers can just code against the API contract and easily switch of the concrete implementation "under the hoods" without changing the code. The JDBC API is an excellent example. It exist of almost only interfaces...