Thursday, August 21, 2014

My First jQuery Program

What is jQuery
jQuery is nothing but JavaScript library that comes with rich functionalities. It's small and faster than many JavaScript code written by average web developer. By using jQuery we can write less code and do more things, its makes web developer's task very easy. In simple word jQuery is a collection of several useful methods, which can be used to accomplish many common tasks in JavaScript. Couple of lines of jQuery code can do thing which need too many JavaScript lines to accomplish. True power of jQuery comes from it's CSS like selector, which allows it to select any element from DOM and modify, update or manipulate it. You can use jQuery to do cool animations like fade in or fade out. You can also change CSS class of a component dynamically e.g. making a component active or inactive. I have used this technique to implement tabbed UI in HTML. I can vouch for jQuery that once you start using it, you will never go back to plain old JavaScript, it's clear, concise and powerful. You will even regret why you are not using jQuery before.

Wednesday, August 13, 2014

4 Examples to Sort Array in Java

You can use Arrays.sort() method to sort both primitive and object array in Java. This method sorts given array into ascending order, which is numeric order for primitives and defined by compareTo() or compare() method for objects. For primitive arrays e.g. int,  short, character, float, double or long this method uses  dual-pivot Quicksort sorting algorithm implemented by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloach (author of Effective Java) . This algorithm offers O(n log(n)) performance on many data sets that cause other quicksort algorithms to degrade into their worst quadratic performance e.g. O(n^2), and is typically faster than traditional (one-pivot) Quicksort implementations. That's why I always said that prefer library method your own, you can get it right but amount of exposure library method gets, you will never get for your implementations. On the other hand object array is sorted using stable MergeSort algorithm, which ensures that equal elements keep their original position in sorted array. Implementation of mergesort used in sort(Object[]) is stable, adaptive, and iterative that requires much lesser than O(n log(n)) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. In best case, when input array is almost sorted, this implementation requires approximately O(n) comparisons. By the way temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays. In order to sort different types of array in Java, you can use any of the overloaded version of sort() method from Arrays class. It also has two special method for sorting object array, one sorts the array in natural order, while other sort them in custom order of provided comparator. Since two dimensional array is also array of array in Java, you can use any this method to sort multi dimensional array in Java also. We will see step by step examples of sorting all kinds of array in Java in subsequent section.

Monday, August 11, 2014

Difference between POST and GET Request in HTTP Protocol

HTTP Protocol supports many method to retrieve data from server or perform any operation on server e.g. upload data, delete file etc. In total, HTTP protocol supports following methods e.g. GET, POST, PUT, DELETE, HEAD, DELETE, OPTIONS and TRACE and HTTP 1.1 reserves method called CONNECT for future use.  GET and POST are two of the most common HTTP methods you would heard or work in web. Though both can be used to send and receive data from client to server, there are some important difference between GET and POST in HTTP, which will help you to understand when you should use GET vs POST while writing your client and server application. HTTP is also programming language independent, doesn't matter whether your client and server is written in Java, or client written in HTML, JavaScript and Server in Java, or client and server both written in .NET, you will use HTTP protocol. In this article, we will learn pros and cons of GET and POST method to choose, which method you should use in HTML forms, considering facts like security, speed and amount of data to transfer.

Java67 Headline Animator