Tuesday 31 May 2016

What are Idempotent and Safe methods of HTTP and REST

In order to efficiently work with REST and RESTful web service, good knowledge of HTTP is really helpful. Even though REST seems easy, designing a uniform and consistent RESTful API is a tough job. One of the tricky tasks is choosing right the HTTP method for right job e.g. when to use PUT vs POST. Once you know the meaning and purpose of different HTTP methods, it helps to choose the right method for the right job. You can divide HTTP methods into two main categories safe and idempotent. Safe methods are HTTP methods that do not modify the resource e.g. a GET request  is safe because it doesn't modify the resource you are requesting e.g. data of a Book. Another safe HTTP method is HEAD, which doesn't change the resource representation on the Server, but all other HTTP methods e.g. POST, PUTor DELETE are non-safe.
Read more »

Sunday 29 May 2016

Minecraft - java.lang.UnsatisfiedLinkError: lwjgl64.dll : Access Denied Solution

You can resolve java.lang.UnsatisfiedLinkError: lwjgl64.dll : Access Denied error in Minecraft by disabling your anti-virus and run. Later you can whitelist the lwjgl64.dll, so that your anti-virus will not block it again. I have talked about java.lang.UnsatisfiedLinkError couple of times on this blog e.g. here and here. But, today I am going to show you one more real life example of java.lang.UnsatisfiedLinkError, which is more interesting. We'll also learn and how to deal with that. This problem is related to Minecraft, one of the most popular game written in Java. Precisely, we are going to solve the "Exception in thread "main" java.lang.UnsatisfiedLinkError: lwjgl64.dll: Access denied" error.
Read more »

Saturday 28 May 2016

How to reverse an ArrayList in place in Java - Example

You can reverse an ArrayList in place in Java by using the same algorithm we have used to reverse an array in place in Java. If you have already solved that problem then It's a no-brainer because ArrayList is nothing but a dynamic array, which can resize itself. All elements of an array are stored in the internal array itself. By the way, if you need to reverse an ArrayList then you should be using the Collections.reverse() method provided by Java Collection framework. It's generic method, so you can not only reverse an ArrayList but also Vector, LinkedList, CopyOnWriteArrayList, or any other List implementation. Though, worth noting is that this method internally uses a ListIterator for reversing the list, which might not be as efficient as our algorithm.
Read more »

Thursday 26 May 2016

Best practices to name your JAR file in Java

If you are an author of an internal, proprietary Java library or an external open source library,  or you are one of those lucky developers who ship Java application by yourself then you should follow these best practices while naming your JAR files. These best practices are a result of the practical experience of using hundreds of Java library and application. Following these best practices will help  in better management of JAR files. It's part of my other best practices articles e.g. best practices while naming variable, writing comments, overriding methods, muli-threading, JDBC, and best practices while dealing with passwords. If you are interested in learning more best practices, you can always search those articles on this blog.
Read more »

Monday 23 May 2016

5 Free Data Structure and Algorithm Books in Java Programming

In last article, I have shared 5 good data structure and algorithms books, but those were not free. After that article, I received some feedback about how about free data structure and algorithm books? Fair enough, everybody loves free eBooks, don't you? In the past, I have shared a list of free Java programming books, so I had some idea. I did some more research on internet and checked my collection as well. Fortunately, there are a couple of good Data Structure and Algorithm books which are available for free PDF download or for online reading and in this list we will see some of them. Data Structure and Algorithm is one of the most important topics for any programmer, not just for interview point of view but also about writing good programs and problem solving. A good choice of data structure can reduce the complexity of the algorithm and can improve its performance drastically.
Read more »

Saturday 21 May 2016

Java Mistake 3 - Using "==" instead of equals() to compare Objects in Java

In this part of Java programming mistakes, we will take a look at another common pattern, where programmers tend to use "==" operator to compare Objects, similar to comparing primitives. Since equality of object can be very different in the physical and logical sense, and in the case of domain objects it's mostly driven by business rules, comparing objects with "==" operator, introduces subtle bugs, which are hard to find. The difference between equals() and == operator,  one of the Java classics is also asked to find out if the developer is familiar with this important concept or not. Using == operator only make sense when comparing primitives like int, or final constants like Enum. Though there is more involved in comparing two Enum, which you learn by following that link.
Read more »

What is difference between Synchronized and Concurrent Collections in Java?

Synchronized vs Concurrent Collections
Though both Synchronized and Concurrent Collection classes provide thread-safety, the differences between them comes  in performance, scalability and how they achieve thread-safety. Synchronized collections like synchronized HashMap, Hashtable, HashSet, Vector, and synchronized ArrayList are much slower than their concurrent counterparts e.g. ConcurrentHashMap, CopyOnWriteArrayList, and CopyOnWriteHashSet. Main reason for this slowness is locking; synchronized collections locks the whole collection e.g. whole Map or List while concurrent collection never locks the whole Map or List. They achieve thread safety by using advanced and sophisticated techniques like lock stripping. For example, the ConcurrentHashMap divides the whole map into several segments and locks only the relevant segments, which allows multiple threads to access other segments of same ConcurrentHashMap without locking.
Read more »

How do you find length of a Singly Linked list using Loop and Recursion

Hi Guys,
Here is one of the classical programming questions asked to me first time on an interview with multinational Investment bank. After that, this question has been asked to me on several occasions in other programming job interviews as well. What makes this question interesting is that Java developers are not that great with the data structure as compared to C++ developer which is obvious because of the fundamental difference between these two languages. The C++ is more of system programming language while Java is more on application programming , also a rich set of Java API allows the programmer to skip this kind of basic programming techniques.
Read more »

Thursday 19 May 2016

Command design Pattern in Java with Example

In simple words, Command design pattern is used to separate a request for an action from the object which actually performs the action. This decoupling between Invoker and Receiver object provides a uniform ways to perform different types of actions. This decoupling is achieved using a Command object, which is usually an interface with methods like execute(). The Requestor or Invoker only knows about Command object, and doesn't care of the actual object which process the request, which can be different. This transparency leads clean code on Invoker side and also enables opportunity to do several smart things on Command side. Your Command object can be as dumb as simply delegating request to Receiver and can be as smart as recording last command for performing UNDO and REDO functionality.
Read more »

Monday 16 May 2016

Difference between float and double variable in Java?

Though both float and double datatype are used to represent floating point numbers in Java, a double data type is more precise than float. A double variable can provide precision up to 15 to 16 decimal points as compared to float precision of 6 to 7 decimal digits. Another significant difference between float and double is their storage requirement, double is more expensive than float. It takes 8 bytes to store a variable while float just takes 4 bytes. Which means, if memory is constraint than its better to use float than double. BTW, the double type also has larger range than float and if your numbers don't fit well in float then you have to use double in Java. It's also worth noting that floating point numbers or real numbers are by default double in Java. If you want to store them into float variable, you need to either cast them or use a prefix 'f' or 'F' as shown in our example.
Read more »

Thursday 12 May 2016

Difference between close and deallocate cursor in SQL

Cursor in a database is used to retrieve data from the result set, mostly one row at a time. You can use Cursor to update records and perform an operation on a row by row. Given its importance on SQL and Stored procedure, Cursor is also very popular on SQL interviews. One of the popular SQL question on Cursor is close vs deallocate. Since both of them sounds to close the cursor, once the job  is done, What is a real difference between close and deallocate of Cursor in SQL? Well, there is some subtle difference e.g. closing a cursor doesn't change its definition. In Sybase particular, you can reopen a closed cursor and when you reopen it, it creates a new cursor based upon the same SELECT query. On the other hand, deallocation a cursor frees up all the resources associated with the cursor, including cursor name. You just cannot reuse a cursor name by closing it, you need to deallocate it. By the way, if you deallocate an open cursor, it's get closed automatically. Similarly terminating database connection from Server, also closes and deallocates any open cursors.
Read more »