Search This Blog

Loading...

Thursday, July 30, 2015

HashSet vs TreeSet in Java? Similarities and Differences

HashSet and TreeSet both implement same interface i.e  java.util.Set interface and they posses the quality of Set interface means duplicate elements are not allowed. Both HashSet and TreeSet are used for to store unique elements, but HashSet doesn't care about any order and TreeSet keeps thing in order. Ordering or sorting on TreeSet can be customized by using Comparator interface, by default TreeSet uses elements natural order for sorting, which is defined by compareTo() method of java.lang.Comparable interface. What is difference between HashSet and TreeSet is is also one the frequently asked Java interview question, So you should know about similarities and difference between them. It also helps you to understand when to use both TreeSet and HashSet and whats are the scenario when we should use this sets. Let's go through the similarities and difference between HashSet and TreeSet in Java.

Wednesday, July 29, 2015

How to join two threads in Java? Thread.join() example

You can join two threads in Java by using join() method from java.lang.Thread class. Why do you join threads? because, you want one thread to wait for another before starts processing. It's like a relay race where second runner waits until first runner comes and hand over flag to him. Remember, unlike sleep(), join() is not a static method so you need an object of java.lang.Thread class to call this method. Now, who calls and who waits and which thread dies? for example, if you have two threads in your program main thread and T1 which you have created. Now if your main thread execute this line T1.join() (main thread execute whatever you have written in main method) then main thread will wait for T1 thread to finish its execution. Immediate effect of this call would be that main thread will stop processing immediately and will not start until T1 thread has finished. So, what will happen if T1 is not yet started and there is no one to start T1 thread? Well,  then you have a deadlock but if its already finished then main thread will start again, provided it get the CPU back. In this tutorial, you will learn how to make three threads execute in a order by using join() method. You an also check out Java Threads By Scott Oaks and Henry Wong to get a good starting overview of several fundamentals of threads and multi-threading.

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 System.in, 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.