Java Program to Perform Binary Search in Array without Recursion

The binary search algorithm is used to search an element in the sorted array. It's faster than linear search and improves performance from O(n) to O(logN) for searching an element in the array. In order to perform the binary search, you need a sorted array, so you can either ask the user to enter array in sorted order or you should sort the array before performing the binary search. In this article, we will write a Java program which will take input from the user, both array and the number to be searched and then perform a binary search to find that number in given array. We'll not use the Collections.binarySearch() method instead we'll write our own because it's a programming exercise to test one's coding skill. In order to implement binary search, you must first know how binary search works? If you don't know the algorithm you cannot code it. So, let's first revise the binary search algorithm itself.

keySet() vs entrySet vs values() Example in Java Map

The java.util.Map interface provides three methods keySet(), entrySet() and values() to retrieve all keys, entries (a key-value pair), and values. Since these methods directly come from the Map interface, you can use it with any of the Map implementation class e.g. HashMap, TreeMap, LinkedHashMap, Hashtable, ConcurrentHashMap, and even with specialized Map implementations like EnumMap, WeakHashMapand IdentityHashMap. In order to become a good Java developer, it's important to understand and remember key classes Java API e.g. Java's Collection framework. In this article, we will not only learn the difference between keySet(), entrySet() and values() methods, but also learn how to use them in Java program by looking at a simple example.

Java Program to calculate Area of Circle

You can calculate the area of a circle in Java by just writing a class and a method. All you need to know is the formula to calculate the area of circle and trick to get input from the user in Java. If you know these two already than the calculating area of a circle is very easy. Since every program must have a class in Java, we need to create a class. I have created a class called Circle for our examples purpose. Now, since the execution of Java program starts from the main method, I have provided a public static void main() method in our program. This single method is enough to put all the code required for this program e.g. getting input from the user, calculating area and displaying area of a circle in the console. But, for better coding experience purpose, we'll just create a method to calculate the area of a circle.

Fibonacci Series in Java Using Recursion

Fibonacci series in Java
Write a Java program to print Fibonacci series up to a given number or create simple Java program to calculate Fibonacci number is common Java questions on fresher interview and homework. Fibonacci series is also a popular topic on various programming exercises in school and colleges. Fibonacci series is series of natural number where next number is equivalent to the sum of previous two number e.g. fn = fn-1 + fn-2. The first two numbers of Fibonacci series is always 1, 1. In this Java program example for Fibonacci series, we create a function to calculate Fibonacci number and then print those numbers on Java console. Another twist in this questions is that sometime interviewer asks to write a Java program for Fibonacci numbers using recursion, so it's better you prepare for both iterative and recursive version of Fibonacci number.

Difference between static and nonstatic member variables in Java

In the last article, I had explained about some key difference between static and nonstatic methods in Java, and in this part, I'll explain the difference between static and nonstatic member variables in Java. The concept of static remains same, that doesn't change with method or member variables but there are still some subtle details, which every Java programmer should know and understand. As with static methods, a static member variable belongs to a class and a non-static member variable belongs to an instance. This means, the value of a static variable will be same for all instances, but the value of a non-static variable will be different for different objects. That is also referred as the state of objects. The value of nonstatic member variable actually defines the state of objects.