System.out.printf使用以及注意點

1、System.out.printf格式化輸出

一、經常使用控制符

控制符html

說明編程

%d數組

按十進制整型數據的實際長度輸出。架構

%ldapp

輸出長整型數據。框架

%mdide

m 爲指定的輸出字段的寬度。若是數據的位數小於 m,則左端補以空格,若大於 m,則按實際位數輸出。性能

%u測試

輸出無符號整型(unsigned)。輸出無符號整型時也能夠用 %d,這時是將無符號轉換成有符號數,而後輸出。但編程的時候最好不要這麼寫,由於這樣要進行一次轉換,使 CPU 多作一次無用功。優化

%c

用來輸出一個字符。

%f

用來輸出實數,包括單精度和雙精度,以小數形式輸出。不指定字段寬度,由系統自動指定,整數部分所有輸出,小數部分輸出 6 位,超過 6 位的四捨五入。

%.mf

輸出實數時小數點後保留 m 位,注意 m 前面有個點。

%o

以八進制整數形式輸出,這個就用得不多了,瞭解一下就好了。

%s

用來輸出字符串。用 %s 輸出字符串同前面直接輸出字符串是同樣的。可是此時要先定義字符數組或字符指針存儲或指向字符串,這個稍後再講。

%x(或 %X 或 %#x 或 %#X)

以十六進制形式輸出整數,這個很重要。

代碼演示:

public static void main(String[] args) {
2         //最經常使用的主要是三個:字符串 %s, 整型%d, 浮點型保留小數位%.mf(m表示小數點後m位), \n表示換行符
3         System.out.printf("*學生資料*\n 姓名:%s\n 年齡:%d歲\n 考試成績(保留兩位小數): %.2f\n", 
                            "小明", 15, 98.456);
4     }

控制檯顯示:

另外System.out.printf有必定程度的輸出格式化效果

輸出結果:

若是 使用System.out.print(ln)格式就出現了明顯不一樣

二、示例代碼

package system.out;
 
public class Printf
{
 
    public static void main(String[] args)
    {
        //%表明格式化
        //f表明輸出浮點數,9表明輸出長度,若是浮點數長度不足,則補空格,若是浮點數長度超出,則按實際長度輸出,2表明保留小數點後幾位小數
        System.out.printf("%9.2f",1111.3);
        System.out.println();
        //-號表明向左對齊,默認向右對齊
        System.out.printf("%-9.2f", 1111.3);
        System.out.println();
        //+號表明顯示正負號
        System.out.printf("%+9.2f", 1111.3);
        System.out.println();
        //+-號表明顯示正負號,且向左對齊
        System.out.printf("%+-9.2f", 1111.3);
        System.out.println();
        //d表明輸出整數
        System.out.printf("%4d",15);
        System.out.println();
        //o表明輸出8進制整數
        System.out.printf("%-4o",15);
        System.out.println();
        //x表明輸出16進制整數
        System.out.printf("%-4x",15);
        System.out.println();
        //#x表明輸出帶有16進制標誌的整數
        System.out.printf("%#x",15);
        System.out.println();
        //s表明輸出字符串
        System.out.printf("%-8s", "咱們是中心");
        System.out.println();
        //x$,整數加$表示第幾個變量,若是不加,變量按默認順序排列
        System.out.printf("%2$-5s:奪得世界盃總冠軍,進球數:%1$3d,對方進球:%3$2d", 4,"法國",2);
    }
}

 


2、看下底層代碼實現

public PrintStream printf(String format, Object ... args) {
        return format(format, args);
    }
public PrintStream format(String format, Object ... args) {
        try {
            synchronized (this) {
                ensureOpen();
                if ((formatter == null)
                    || (formatter.locale() != Locale.getDefault()))
                    formatter = new Formatter((Appendable) this);
                formatter.format(Locale.getDefault(), format, args);
            }
        } catch (InterruptedIOException x) {
            Thread.currentThread().interrupt();
        } catch (IOException x) {
            trouble = true;
        }
        return this;
    }

能夠看到和String.format底層實現相似調用Formatter()類的format方法。

public static String format(String format, Object... args) {
        return new Formatter().format(format, args).toString();
    }

 


3、接下來分析下 String.format與StringBuilder與String +比較

一、測試代碼:

class StringTest {

    public static void main(String[] args) {
       //  testOperatorPlus();
        //testAppend();
       testFormat();
    }

    private static void testFormat() {
        Runtime runtime = Runtime.getRuntime();
        long memory;
        long prev_time;
        int i;
        long time;
        StringBuilder sb = new StringBuilder();
        memory = runtime.freeMemory();
        prev_time = System.currentTimeMillis();
        for (i = 0; i < 10000; i++) {
            String s = String.format("Blah %d Blah %d Blah %d", i, i, i);
        }
        long ww=runtime.freeMemory();
        time = System.currentTimeMillis() - prev_time;
        memory = memory - ww;
        System.out.println("Time: " + time + "    Memory Usage: " + memory);
    }

    private static void testAppend() {
        Runtime runtime = Runtime.getRuntime();
        long memory;
        long prev_time;
        int i;
        long time;
        StringBuilder sb = new StringBuilder();
        memory = runtime.freeMemory();
        prev_time = System.currentTimeMillis();
        for (i = 0; i < 10000; i++) {
            sb.append("Blah ");
            sb.append(i);
            sb.append("Blah ");
            sb.append(i);
            sb.append("Blah ");
            sb.append(i);
        }
        time = System.currentTimeMillis() - prev_time;
        memory = memory - runtime.freeMemory();
        System.out.println("Time: " + time + "    Memory Usage: " + memory);
    }

    private static void testOperatorPlus() {
        Runtime runtime = Runtime.getRuntime();
        long memory;
        long prev_time;
        int i;
        long time;
        StringBuilder sb = new StringBuilder();
        memory = runtime.freeMemory();
        prev_time = System.currentTimeMillis();
        for (i = 0; i < 1000000; i++) {
            String s = "Blah " + i + "Blah " + i + "Blah " + i;
        }
        time = System.currentTimeMillis() - prev_time;
        memory = memory - runtime.freeMemory();
        System.out.println("Time: " + time + "    Memory Usage: " + memory);
    }
}
View Code

結果以下

Method Time(ms) Memory Usage(long)
‘+’ operator 102 44053736
StringBuilder.append 6 884768
String.foramt 110 22639000

 

 

 

 

  
  
  能夠看到 StringBuilder.append的執行時間和內存佔用都是最優的。'+'運算符比直接調用 StringBuilder.append要慢上很多,特別是要鏈接的字符串數量較多時,內存佔用也特別大。 String.format因爲每次都有生成一個 Formatter對象,較慢也是情理之中。
分析下String.format源碼能夠看到底層也用到了StringBuilder
public static String format(String format, Object... args) {
        return new Formatter().format(format, args).toString();
    }
public Formatter() {
        this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder());
    }

4、由此引起的優化探討

在編碼中 System.out.println將對象結果輸出到控制檯,會花費大量的CPU資源,所以發佈的代碼中不要包含System.out.println 或 System.out.printf。

使用日誌框架代替,生產環境注意控制輸出級別。

即使使用日誌框架也要注意輸出編碼方式,例如
反例(不要這麼作):
logger.debug("Processing trade with id: " + id + " symbol: " + symbol);

字符串拼接,這樣會產生不少String對象,佔用空間,影響性能。

另外關於日誌輸出其餘建議:

一、使用[]進行參數變量隔離

若有參數變量,應該寫成以下寫法:

logger.debug("Processing trade with id:[{}] and symbol : [{}] ", id, symbol);
這樣的格式寫法,可讀性更好,對於排查問題更有幫助。
二、並非全部的service都進行出入口打點記錄,單1、簡單service是沒有意義的(job除外,job須要記錄開始和結束,)。
反例(不要這麼作):
public List listByBaseType(Integer baseTypeId) {
 log.info("開始查詢基地");
 BaseExample ex=new BaseExample();
 BaseExample.Criteria ctr = ex.createCriteria();
 ctr.andIsDeleteEqualTo(IsDelete.USE.getValue());
 Optionals.doIfPresent(baseTypeId, ctr::andBaseTypeIdEqualTo);
 log.info("查詢基地結束");
 return baseRepository.selectByExample(ex);
}

對於複雜的業務邏輯,須要進行日誌打點,以及埋點記錄,好比電商系統中的下訂單邏輯,以及OrderAction操做(業務狀態變動)。

若是全部的service爲SOA架構,那麼能夠當作是一個外部接口提供方,那麼必須記錄入參。
調用其餘第三方服務時,全部的出參和入參是必需要記錄的(由於你很難追溯第三方模塊發生的問題)

三、 生產環境須要關閉DEBUG信息

若是在生產狀況下須要開啓DEBUG,須要使用開關進行管理,不能一直開啓。

 
 
 
參考文章:
相關文章
相關標籤/搜索