Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.printf vs System.out.format

Are System.out.printf and System.out.format totally the same or perhaps they differ in somehow?

like image 250
Rollerball Avatar asked Apr 26 '13 10:04

Rollerball


People also ask

What is the difference between format and printf?

The key difference between them is that printf() prints the formatted String into console much like System. out. println() but the format() method returns a formatted string, which you can store or use the way you want.

What is system out printf?

System.out.printf("The String object %s is at hash code %h%n", s, s); String class format( ) method: You can build a formatted String and assign it to a variable using the static format method in the String class. The use of a format string and argument list is identical to its use in the printf method.

What is the use of system out format?

System. out. format("The value of " + "the float variable is " + "%f, while the value of the " + "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar); The first parameter, format , is a format string specifying how the objects in the second parameter, args , are to be formatted.

Is string format the same as printf Java?

Using printf() , String. format() or Formatter is essentially the same thing. The only thing that differs is the return type - printf() prints to the standard output stream (typically your console) and String.


2 Answers

System.out is a PrintStream, and quoting the javadoc for PrintStream.printf

An invocation of this method of the form out.printf(l, format, args) behaves in exactly the same way as the invocation out.format(l, format, args)

like image 68
NilsH Avatar answered Oct 03 '22 08:10

NilsH


The actual implementation of both printf overloaded forms

public PrintStream printf(Locale l, String format, Object ... args) {     return format(l, format, args); } 

and

public PrintStream printf(String format, Object ... args) {         return format(format, args); } 

uses the format method's overloaded forms

public PrintStream format(Locale l, String format, Object ... args) 

and

public PrintStream format(String format, Object ... args) 

respectively.

like image 38
c.P.u1 Avatar answered Oct 03 '22 09:10

c.P.u1