Search This Blog


Monday, July 27, 2015

OCAJP 7 or OCAJP 8? Which Java Certification should I take?

Java certification is great way to learn and master The Java programming language, do well on interview and get recognition in job, but most common reason of doing Java certification is to find a job. After Oracle's acquisition and dividing old SCJP exam into two exams OCAJP and OCPJP, it has become really confusing for many developers to decide which Java certification exam they should take. Since technologies change fast and Java is no exception, its just an year when Java 8 was released and now people are talking about Java 9. In general, it is better to go with the latest version of a certification, which is currently available i.e. the Java 8 version. But in case of Java certifications, it's not that simple. Many of my reader email me about my advice to decide between Java SE 6, 7 and 8 exams before zeroing one. After answering their emails, I realize its better to jolt down important points for everyone's benefit. Here are things you should consider before buying Java certification voucher and starting your preparation.

Friday, July 24, 2015

22 Array Concepts Interview Questions Answers in Java

Array is one of the fundamental data structure and most of other data structures are based upon it e.g. List, Hash tables are based upon arrays. Array based questions are quite popular on Java interviews. There are two types of question you will find, first which are based upon array implementation in Java and second which are based upon how to use array data structure to solve some problems. First type of question is quite popular in telephonic round of Java interview and second is usually asked on written test and face-to-face interviews. These questions are mostly collected from Java interviews for junior and intermediate programmers, who has 0 to 5 years of working experience in Java. Some tricky questions like question no 2 and 15 are also good for senior and more experienced Java professionals. In fact, one of my friend didn't got the question 15 right, he was actually surprised by the fact that you can make an array volatile in Java. Anyway, if you are seriously preparing for Java interview then you have to do more than just preparing for array based questions. I suggest you to take a look at Cracking the Coding Interview book, which contains 150 Programming Questions and Solutions, good enough to clear any Java interview. If you want to prepare more Java specific questions then you can also take a look at Java Programming Interview Exposed, which contains lots of questions from Java and related technology e.g. Maven, JUnit, Servlet, JSP, Android and others.

Thursday, July 23, 2015

How to calculate Maximum and minimum in Java? Beginner Tutorial

Today's programming exercise for beginner is to write a Java program to take input from user and find out maximum and minimum number and print them into console. Purpose of this article is to teach you how to get input from user in Java and how to use java.lang.Math class to perform some mathematical operation e.g. max, min or average. You can use Scanner class, added in Java 1.5 to read user input from console. Scanner needs an InputStream to read data and because you are reading from console, you can pass, which is InputStream for Eclipse console, or command prompt, depending upon what you are using. This class also helps you to convert user input into require data type e.g. if user enter numbers then you must convert then into int data type and store them into int variable as shown in our example. You can use nextInt() method to read user input as Integer. Similarly you can use nextLine() to read user input as String. There are other methods available to read float, double, or boolean from command prompt. Once you got both the numbers, it just matter of using relational operator less than and greater than to find smaller and larger number, as shown in following example.  After that you can Math.max() to find maximum of two numbers, it should be same as your earlier result. Similarly, we will ask User to enter number again and will display minimum of two. I also suggest you to read Head First Java 2nd Edition book, if you have just started learning Java. It is one of the best book to learn Java in quick time.

Monday, July 20, 2015

How to stop a thread in Java? Example

Today we're going to learn about how to stop a thread in Java. It's easy to start a thread in Java because you have a start() method but it's difficult to stop the thread because there is no working stop() method. Well, there was a stop() method in Thread class, when Java was first released but that was deprecated later. In today's Java version, You can stop a thread by using a boolean volatile variable.  If you remember, threads in Java start execution from run() method and stop, when it comes out of run() method, either normally or due to any exception. You can leverage this property to stop the thread. All you need to do is,  create a boolean variable e.g. bExit or bStop. Your thread should check its value every iteration and comes out of loop and subsequently from run() method if bExit is true. In order to stop the thread, you need to set value of this boolean variable to true when you want to stop a running thread. Since you are setting this variable from a different thread e.g. main thread, its important to mark this variable volatile, otherwise its possible for the running thread to cache its value and never check back to main memory for updated value and running infinitely. When you make a variable volatile, running thread will not cache its value in its local stack and always refer main memory. Volatile variable also provides happens before guarantee, which can be used to synchronize values. If you want to understand multi-threading and concurrency in right way, I strongly suggest to read Java Concurrency in Practice twice. Its one of the most accurate, thorough and practical book in Java multi-threading.

Sunday, July 19, 2015

How to search an element inside LinkedList in Java? Example

You can search an element inside LinkedList in Java by using indexOf() and lastIndexOf() methods. Though LinkedList doesn't support random search like ArrayList, you can still go through the list, check each element and find out whether its interested element or not. Since java.util.LinkedList is an implementation of doubly linked list, these two methods are quite handy to search from either ends e.g. indexOf() method start search from head and return an elements position, while lastIndexOf() starts search from tail. Though, position is not relative to ends, they are always calculated from head. You can also use these two methods to find out duplicate elements. If an element is appeared twice in linked list then indexOf() and lastIndexOf() method will return different positions for that because it will be found at different position from head and tail. For unique elements, both these methods will return same position. In this article, you will see examples of both indexOf() and lastIndexOf() methods to search a given element inside LinkedList. As I said before, since LinkedList doesn't support random search and searching an element require list traversal, which means time complexity will be O(n). BTW, if you are good in Java but lacks data structure and algorithm skill, I strongly suggest to read Data Structures and Algorithm Analysis in Java by Mark A. Wiess. It's a great book to build your foundation on data structure and algorithm using Java programming language.

Thursday, July 16, 2015

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]

Problem : You are getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error while connecting to MySQL database from Java Program. You may be running your Java application directly from command prompt, shell script, ANT or Eclipse.

Cause : In order to connect to MySQL database, you need JDBC driver for MySQL. A class which implements java.sql.Driver interface for MySQL. Every vendor is responsible to implement this class for their databases. This driver implementation is provided by MySQL as MySQL java connector library. There is a class called com.mysql.jdbc.Driver which implements this interface.