Chapter 13 - Exercise 1

(2) Analyze SprinklerSystem.toString( ) in reusing/SprinklerSystem.java to discover whether writing the toString( ) with an explicit StringBuilder will save any StringBuilder creations.


第二种toStringBuilder()方法没有减少StringBuilder的数量。原因是第一个toString()方法已经优化过,也只产生了一个StringBuilder对象。因为没有在循环里拼接String,所以没有产生很多中间StringBuilder。

Exercise1.java



package com.ciaoshen.thinkinjava.chapter13;
import java.util.*;

public class Exercise1{
    private final String valve1="Monday";
    private final String valve2="Tusday";
    private final String valve3="Wendsday";
    private final String valve4="Thursday";
    private WaterSource source = new WaterSource();
    private int i;
    private float f;

    //use String
    //每进行一次“+”操作,就产生一个StringBuilder对象。
    public String toString() {
        long begin=System.nanoTime();
        String result=
        "valve1 = " + valve1 + " " +
        "valve2 = " + valve2 + " " +
        "valve3 = " + valve3 + " " +
        "valve4 = " + valve4 + "\n" +
        "i = " + i + " " + "f = " + f + " " +
        "source = " + source;
        long end=System.nanoTime();
        System.out.println((end-begin)/10e6);
        return result;

    }

    //use StringBuilder
    //只产生一个StringBuilder对象
    public String toStringBuilder() {
        long begin=System.nanoTime();
        StringBuilder result=new StringBuilder();
        result.append("valve1 = ").append(valve1).append(" ");
        result.append("valve2 = ").append(valve2).append(" ");
        result.append("valve3 = ").append(valve3).append(" ");
        result.append("valve4 = ").append(valve4).append("\n");
        result.append("i = ").append(i).append(" ").append("f = ").append(f).append(" ").append("source = ").append(source);
        long end=System.nanoTime();
        System.out.println((end-begin)/10e6);
        return result.toString();
    }
    public static void main(String[] args) {
        Exercise1 sprinklers = new Exercise1();
        System.out.println(sprinklers);
        System.out.println(sprinklers.toStringBuilder());
    }
}


WaterSource.Java



package com.ciaoshen.thinkinjava.chapter13;

public class WaterSource {
    private String s;
    public WaterSource() {
        System.out.println("WaterSource()");
        s = "Constructed";
    }
    public String toString() { return s; }
}



Need Java Developers? Hire Me »

I know Java. Talk is cheap, watch my code.

I live in CANADA now.