19

May I know what is the difference between the two in java? I am reading a book and it uses both methods to display strings.

Abra
  • 19,142
  • 7
  • 29
  • 41
sutoL
  • 1,737
  • 10
  • 27
  • 48

6 Answers6

25

The first one writes to the stdout and the second one returns a String object.

Which to use depends on the sole purpose. If you want to display the string in the stdout (console), then use the first. If you want to get a handle to the formatted string to use further in the code, then use the second.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Both methods print to the console. What's the difference? – K Man Aug 21 '19 at 13:38
  • The second one doesn't print to the console. Probably you did it yourself via e.g. `System.out.println(String.format(string, param))`. – BalusC Aug 22 '19 at 09:58
8

String.format returns a new String, while System.out.printf just displays the newly formatted String to System.out, sometimes known as the console.

These two snippets of code are functionally equivalent:

String formattedString = String.format("%d is my favorite number", 42);
System.out.print(formattedString);

and

System.out.printf("%d is my favorite number", 42);
Kartik Chugh
  • 1,104
  • 14
  • 28
Greg Case
  • 3,200
  • 1
  • 19
  • 17
  • 7
    Even though this post is really old, I just want to add one thing to your answer: The two snippets are not exactly equivalent. System.out.printf does not insert a new line. – Quintec May 02 '15 at 19:34
  • I tried both but it seems neither printf nor format inserts a new line – S Kumar Jun 06 '17 at 06:43
  • @SKumar Correct, but 'thecoder16' is referring to Greg's use of `println`, which, when printing a string, prints said string then terminates the line by adding a line separator (essentially a new line). The use of `printf` doesn't, and neither does the `.format` method. – Ghoti and Chips Nov 23 '17 at 00:14
1

String.format formats strings, doesn't display them. I think you mean System.out.println(String.format("......", ....)) or some similar construct ?

laura
  • 7,280
  • 4
  • 35
  • 43
1

String.format returns a formatted string. System.out.printf also prints the formatted string.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
1

These two methods exhibit the exact same behavior. We can use the format(...) with String, Java.util.Formatter (J2SE 5) and also with PrintWriter.

naeemgik
  • 2,232
  • 4
  • 25
  • 47
0

The key different between System.out.printf() and String.format() methods is that

  • printf() prints the formatted String into console much like System.out.println() but
  • format() method return a formatted String, which you can store or use the way you want.

Otherwise nature of use is different according to their functionalities -

A sample example to add leading zeros into a numbers :

int num = 5;      
String str = String.format("%03d", num);  // 0005    
System.out.printf("Original number %d, leading with zero : %s", num, str);
Jimmy
  • 995
  • 9
  • 18