Java: Out with the Old, In with the New

后端 未结 30 2035
遥遥无期
遥遥无期 2020-12-22 16:11

Java is nearing version 7. It occurs to me that there must be plenty of textbooks and training manuals kicking around that teach methods based on older versions of Java, whe

30条回答
  •  遥遥无期
    2020-12-22 16:35

    Formatted printing was introduced as late as in JDK 1.5. So instead of using:

    String str = "test " + intValue + " test " + doubleValue;
    

    or the equivalent using a StringBuilder,

    one can use

    String str = String.format("test %d test %lg", intValue, doubleValue);
    

    The latter is much more readable, both from the string concatenation and the string builder versions. Still I find that people adopt this style very slowly. Log4j framework for example, doesn't use this, although I believe it would be greatly benefited to do so.

提交回复
热议问题