Sunday, November 23, 2014

How to test if an Array contains a value in Java - Linear Search

One of the common coding question from Java interviews is how to test if an Array contains a certain value or not? This is simple question but some time interview pressure makes candidates nervous. Since array in Java doesn't have any inbuilt method for search, interviewer prefer to ask this question, to see how candidate deal with such situation. If you have good knowledge of Java API then you will immediately come to know that there are alternatives available e.g. binary search of Arrays class or taking advantage of ArrayList contains method by first converting your array to ArrayList. If you come up with those solution, Interviewer will surely ask you to write down a method to search an element in array without using any library method. You can easily solve this question if you know linear search or binary search algorithm. Linear search is very simple to implement, all you need to do is loop over array and check each value if that is the one or not. Binary search is little tricky but not too difficult either, recursive version is very natural as well. In this tutorial, though I have given two solution, one is using ArrayList, and second is using linear search, leaving binary search an exercise for you. But you must remember to sort array before using binary search. By the way to make question more challenging, I usually asked candidate to write a parametric method using generic so that it will work for any type of object array in Java.

Thursday, November 20, 2014

How to Convert a Double to Long in Java - Example Tutorial

We often need to convert a floating point number into integral number e.g. a double or float value 234.50d to long value 234L or 235L. There are couple of ways to convert a double value to long value in Java e.g. you can simply cast a double value to long or you can wrap a double value into Double object and call it's longValue() method, or using Math.round() method to round floating point value into nearest integer. Te right way to covert a double value to a long in Java really depends upon what you want to do with floating point value. If you just want to truncate the double value to remove zero and take integer value, you can simply cast double to long. If you have Double object instead of double primitive type then you can also Double.longValue() method, this doesn't do anything but just cast the double primitive wrapped inside Double object to long. It's clear from following code snippet taken from java.lang.Double class

 public long longValue() {
        return (long)value;
 }

On the other hand if you want to round the double value to nearest long, you can use Math.round() method, this will return a long value rounded up to nearest position, for example if double value is 100.5 then it will rounded to 101, while if it is less than that e.g. 100.1 then it will be rounded to just 100. In next section we will see detailed examples of these 3 ways to convert double to long in Java.

Tuesday, November 18, 2014

Modulo or Remainder Operator in Java

Modulo Operator is one of the fundamental operator in Java. It's a binary operator i.e. it require two operands. In a division operation the remainder is returned by using modulo operator. It is denoted by % (percentage) sign. For example 5%2 will return 1 because if you divide 5 with 2, remainder will be 1. For a programmer it's very important to know how to use this operator, they are very important to build logic. For example, in many cases like reversing a number or checking if a number is palindrome, you can use modulus operator with 10 to get the last digit, for example 101%10 will return 1 or 1234%10 will return 4, the last digit. It is one of the rather less used arithmetic operator in comparison of +, -, * and /. One of the important point about the remainder operator which is not know by many Java programmer is that it can also be used with floating point numbers. It's surprising because you don't normally think of real number division as producing remainders. However there are some times when it's useful to ask exactly how many times does 2.5 go into 7.5 and what's left over? The answer is that 2.5 goes into 7.5 three times with zero left over, and it's that zero which is the result of 7.5 % 2.5 in Java. Another good use of modulus operator is to check if a number is even or odd. In case of even number, number%2 will return 0, while if a number is odd then number%2 will return 1.

Java67 Headline Animator