What is the difference between printf and print?

11

I was doing some simple algorithms in Java and most of the time using System.out.println() or System.out.print() , but to do the definition of the number of decimal places I'm using System.out.printf() . I know there is a difference between the first two, both in operation and in implementation. But what is the difference between them and the last one?

    
asked by anonymous 16.01.2015 / 00:28

1 answer

11

The most common in Java is to use System.out.print() (or ln if you want it to skip line, but this you already know) but if you need certain formatting use System.out.printf() % may be more appropriate. This method works very much like C by using the printf() .

Example:

System.out.print("x: " + x + " y: " + y);
System.out.printf("x: %d y: %d", x, y);

In addition to being more practical in various situations it can also be very useful where you have text localization or other forms of parameterization of these texts.

    
16.01.2015 / 00:45