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.

Monday, October 13, 2014

2 Examples to Add Zeros at the Beginning of a Number in Java (Padding)

How do you left pad an integer value with zeroes in Java when converting to a string? This is a common requirement if you are working in finance domain. There are so many legacy systems out there which expect input of certain length, and if your input is shorter than specified length, you got to add zeros at the beginning of number to make them of right length. Java has rich API and thankfully neither converting integer to String is difficult nor formatting String to add leading zeros. In fact there are multiple ways to add zeros at the start of a number or numeric String, you can either use powerful String.format() method or it's close cousin printf() method, or you can go back to DecimalFormat class, if you are still working in JDK 4. Formatting in general is a very useful concept and as Java developer you must have a good understanding of that. Earlier we have learned about formatting floating point numbers in Java and that knowledge is going to help a lot. There we have learned about using both String.format() and DecimalFormat to create floating point number up-to two, three or four decimal places, and if you have read that article then you are already familiar with tricky formatting instructions we pass to format() method e.g. "%07d", in this article we will learn how to left pad an integer value in Java by adding zeros at in front of the number.

Wednesday, October 8, 2014

How to use Static Method in Java with Example

Static methods are one of the important programming concept in any programming language but unfortunately it is also most misunderstood and misused one. Talking about Java, almost all programmers knows that. static methods belong to the class and non-static methods belong to the objects of the class, but hardly all of them understand what it mean. That's why this is one of the popular weed out question on programming interviews. If a Java programmer doesn't know difference between static and non-static method, he is probably not ready yet, and needs more practice and experience. With half knowledge, making a method static can have serious repercussion , especially in today's heavily multi-threaded Java application. Today, one of my reader message me this question on my Facebook account of Javarevisited, which prompted me to to write this tutorial cum discussion post to give a good explanation or example to clarify the concept. What you have learned is totally find, all we do here is to elaborate your learning by understanding key differences between static methods and instance methods in Java.

Java67 Headline Animator