difference between System.out.printf and String.format

后端 未结 5 495
野趣味
野趣味 2021-01-04 00:34

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.

5条回答
  •  礼貌的吻别
    2021-01-04 00:56

    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);
    

提交回复
热议问题