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.

Wednesday, October 15, 2014

How to Declare and Initialize Multi-dimensional (2D and 3D) Array in Java with Example

An array of more than one dimension is known as multi-dimensional array. Two of the most common examples of multi-dimensional arrays are two and three dimensional array, known as 2D and 3D array, anything above is rare. I have never seen 4 dimensional arrays, even 3D arrays are not that common. Now question comes, when do use multi-dimensional array? Any real life example? Well, 2D arrays are very common on platform games like Super Mario Bros to represent screen or terrain; 2D arrays can also be used to represent structures like spreadsheet, or to draw board games like Chess, which requires 8x8 board, Checkers and  Tic-Tac-Toe, which requires 3 rows and 3 columns. Another popular application of multi-dimensional arrays are in matrix manipulation. For example to represent a 3x3 matrix you need a two dimensional array of 3 one dimensional array each containing 3 elements. Similarly to represent 3x2 matrices you need 2 two dimensional array of one dimensional array of length 3. In other words, each row in two dimensional array is a one dimensional array.  Java truly doesn't support multi-dimensional array but allows you to create and use array of any number of dimensional. Two dimensional array is actually an array of one dimensional array. This is unlike languages like C or FORTRAN, which allows Java array to have rows of varying length i.e. a multidimensional array can have 2 columns in one row and 3 columns in second. Similar to one dimensional array, length of two dimensional array is also fixed. You can not change length of any array, i.e. number of rows and columns will remain fixed. A 2x2 array can hold total 4 elements and they can be accessed using row and column index e.g. a[0][0] will give you elements in first row and first column, similarly a[1][1] will give you elements from 2nd row and 2nd column. Just like normal array, index start at 0 and finishes at length -1.

Java67 Headline Animator