may i know what is the difference between the two in java? i am reading a book and it can either use both of it to display strings.
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);