Wednesday, January 28, 2015

How to convert Binary Number to Decimal in Java - Algorithm

Problem : Write a Java program to convert a binary number into decimal format, without using any library method which can directly solve the problem. You are free to use basic Java functions though e.g. those defined in java.lang and all kinds of Java operator e.g. arithmetic and logical operator, bitwise and bitshift operator and relational operators.

Solution : Let's first revise some theory of number system, which is required to convert a number from binary to decimal format. There are four kind of number systems binary, octal, decimal and hexadecimal. Binary is base 2 and that's why any number is represented using only two digit, 0 and 1 also known as bits. Octal system is base 8 and you can use 8 digits to represent any number, from 0 to 7. Decimal system is what we human use, it uses 10 digits to represent any number from 0 to 9. Hexadecimal number is base 16 and uses 16 digit to represent a number. Binary is what computer and electronic devices use and Decimal is what we human use. If you remember the algorithm for converting a binary number to decimal in college, you would know that we multiply bits in respective position with 2 to the power of there position, which is zero based. We will use the same algorithm here to convert a binary number into decimal. Only difference is that now we will implement this algorithm in Java. One more thing to remember is that, in order to represent same number you would need more digits in lower base. For example, to represent 8 in binary you need three bits 111, while it only require one digit 8 to represent same number in decimal format. By the way this is the second part of binary to decimal conversion tutorial, in first part we have already seen how to convert a decimal number to binary, so if you have not read it already, check it out.

Tuesday, January 27, 2015

How to use Future and FutureTask in Java Concurrency with Example

Future and FutureTask in Java allows you to write asynchronous code. Future is a general concurrency abstraction, also known as promise, which promises to return a result in future. In asynchronous programming, main thread doesn't wait for any task to finished, rather it hand over the task to workers and move on. One way of aynchronous processing is using callback methods. Future is another way to write asynchronous code. By using Future and FutureTask, you can write method which does long computation but return immediately. Those method, instead of returning result, return a Future object. You can later get result by calling Future.get() method, which will return object of type T, where T is what Future object is holding . One example of Future is submit() method of ExecutorService, which immediately return a Future object. By the way, Future and FutureTask are available in java.util.concurreent package from Java 1.5. Also, Future is and interface and FutureTask is an implementation or RunnableFuture, which can be used as Runnable interface, thus, can be passed to ExecutorService. In this Java concurrency tutorial, we will learn how to use Future and FutureTask in Java.

Monday, January 26, 2015

What is difference between Maven, ANT, Jenkins and Hudson?

In short, though Maven and ANT are build tool but main difference is that maven also provides dependency management, standard project layout and project management. On difference between Maven, ANT and Jenkins, later is a continuous integration tool which is much more than build tool. You can setup your CI environment using Jenkins or Hudson and automatically build, test and deploy your Java project. Now last, main difference between Jenkins and Hudson, both are originate from same source code but one is closed source while other is open source. You can read the details in this article. Now let's start long story, what is difference between Maven and ANT or difference between Maven and Jenkins, or Maven vs Hudson are some of the frequently discussed questions among Java when developers. Well all four e.g.  ANT, Maven, Jenkins and Hudson are tools to help Java developers on build, unit testing, continues integration (CI) and project management. In this Java article we will explore each of Maven, ANT, Jenkins and Hudson to get basic idea of what they are, what benefit they offer and how they are used in Java JEE projects. Just to give you basic idea, ANT is a well known build tool, probably oldest among all. A build tool is used to create deliverable like JAR file or WAR file from Java source and resources for deployment.

Maven came after ANT and offers much more than a build tool. Main difference between ANT and Maven is that In ANT you need to define every thing i.e. source directory, build directory, target directory etc while Maven adopts principle of Convention over configuration. Which means Maven has predefined project structure i.e. standard directory for source files, test files and resources. On the other hand, Jenkins and Hudson are Continues Integration tool, which gives you power to automate your build and deployment process. By using Jenkins or Hudson you can trigger build whenever developer commit code, to see if project is compiling fine, to run unit tests, to create build or even deploy in QA or production environment. Similarly you can have daily build, nightly build or weekly build process established in Jenkins or Hudson. In next section we will understand Maven, ANT, Jenkins and Hudson in more detail and understand difference between them.