Why would one want to use number formatting in Java?

0 Edwin Li · April 5, 2015
Hi! I am completely new to Java and I'm learning. 

I'd like to know the advantages of using number formatting, like this:

public class TestFormat {

public static void main(String[] args) {
long n = 461012;
System.out.format("The value of n is: %d%n", n);

and the result after printing is: The value of n is: 461012. 

Wouldn't it be simpler if I just wrote the actual value of n inside my string? Why would I want to use the percent character?


Post a Reply


Oldest  Newest  Rating
0 Edwin Li · April 9, 2015
Ok, I think I get it -  it's just for more elegant code, but it doesn't actually serve much other use than that.

0 steven kitchener · April 8, 2015
If i understand your question correctly, ultimately it is cleaner code.

Say you have 5 variables called num1 to num5 and you wanted to print them all out with spaces in between them you have two choices.

Use System.out.println(""+num1 + " "+num2+" "+num3+" "+num4+" "+num5); or
        System.out.printf(" %d %d %d %d %d%n",num1,num2,num3,num4,num5);

Both give the same output, but personally i prefer the second choice because of the cleaner look to it. But, to be honest you can use either way. I think if you prefer to use the first method then i dont think there is a problem with that. 
0 Edwin Li · April 8, 2015
Ok, thanks guys.
0 Edwin Li · April 8, 2015
But why can't I write n directly? Why bother to write The of n is: %d%n, n if it gives the same output as if I only wrote The value of n is: n?
+1 Dol Lod · April 6, 2015
I think maybe you should learn about when and how you should hardcode. That being said this topic isn't about that, but like Steven says, it is better practice and simpler. 
+1 steven kitchener · April 6, 2015
The reason you dont want to write the actual value into the string (also known as hardcoding) is simply due to the fact of efficiency.

If you plan to only output the value once, as in this example then yes it would seem that writing 461012 instead of the percent character would be better. But imagine on a larger scale that you output this number 100 times and you put the number in every time. If you were to then change the value of "n" then you would have to manually change all 100 numbers, a tiresome task and very prone to human error. However if you use System.out.format("The value of n is: %d%n", n); then the only change would be to the initial declaration of the variable, quick and simple. So while it seems better to hardcode in this case if you get used to using this type of formatting then you will be able to reduce the lines of code and the amount of changes you will have to make later on down the line. 

Always try and learn the bestway to do something, even if it doesnt seem like the easiest, it'll make your coding life a lot easier.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator