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.

Thursday, August 7, 2014

What is the difference between a Class and an Object in Java?

This article is solely for all beginner programmers, who are learning object oriented programming language e.g. Java, C++ or C# and aspire to do well on any programming interview. Difference between class and object is one of the most common question, you would like to ask a fresher coming out from college or training institute, but you would be surprised how many beginner Java programmers struggle with this question. Class and Object are two pillars of Object Oriented Programming (OOPS) and a good understanding is must, but when you ask this question apart from theoretical and bookish answer that "class is a blueprint and objects are actual things created out of those blueprint", you would hardly get anything substantial. Though that answer is correct and works perfectly, it doesn't differentiate between a programmer, who has just mugged the answer, or the one who truly understand the difference between class and object. So, if I receive that answer, I usually ask them to create a class and explain how objects are created in program, let's say to represent an Employee, Student or simply a Car. If programmer truly understand what is a class and what is an object, it will do that in no time, but if he has just mugged up the answer, then he will be totally confused. Point is to understand the difference between class and object, not to mug up for an interview, Why? because if you know your class and object, it would be lot easy for you to work with an object oriented programming language like Java or C#. To give you some more example of class and object, if Car is a class than Merc, Audi and BMW are objects. If Television is class than Sony Bravia, Samsung Smart tv are its object. If Smartphone is a class then  iPhone, Samsung Galaxy and Nokia Lumia are their object. In an object oriented application, generally nouns are represented using class, for example in finance domain Order, Trade, Instruments are classes. In E-commerce domain Payment, Order, Products are some example of classes.

Java67 Headline Animator