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 the 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 off right length. Java has rich API and thankfully neither converting an 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.

How to use Static Method in Java with Example

Static methods are one of the important programming concepts 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 means. That's why this is one of the popular weed out questions 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.