java编程思想----字符串

字符串

不可变String

public class Immutable {
    public static String upcase(String s){
        return s.toUpperCase();
    }

    public static void main(String[] args) {
        String q = "howdy";
        print(q);
        String qq = upcase(q);
        print(qq);
        print(q);
    }
}/*Output
howdy
HOWDY
howdy
*///~

通过探究toUpperCase()方法来探究upcase()返回的引用已经指向了一个新的对象,而原本的q则还在原地

String类中的toUpperCase()

String类中的toUpperCase()

String类中的toUpperCase()可以看出来这里最后new了一个String

可以看到最后那个toUpperCase中最后return了一个new String所以它最后返回了一个新的对象.由此可以推得其他方法类似,即String对象是不可变的.

13.5格式化输出

  1. System.out.format()说明format方法可用于PrintStream或PrintWriter对象
// 将System.out写到Formatter构造器中相当于System.out.format也就是将这个构造器返回的东西输出到控制台上
public class Turtle {
  private String name;
  private Formatter f;
  public Turtle(String name, Formatter f) {
    this.name = name;
    this.f = f;
  }
  public void move(int x, int y) {
    f.format("%s The Turtle is at (%d,%d)\n", name, x, y);
  }
  public static void main(String[] args) {
    PrintStream outAlias = System.out;
    Turtle tommy = new Turtle("Tommy",
      new Formatter(System.out));
    Turtle terry = new Turtle("Terry",
      new Formatter(outAlias));
    tommy.move(0,0);
    terry.move(4,8);
    tommy.move(3,4);
    terry.move(2,5);
    tommy.move(3,3);
    terry.move(3,3);
  }
} /* Output:
Tommy The Turtle is at (0,0)
Terry The Turtle is at (4,8)
Tommy The Turtle is at (3,4)
Terry The Turtle is at (2,5)
Tommy The Turtle is at (3,3)
Terry The Turtle is at (3,3)
*///:~

格式化说明符:%[argument_index$][flags][width][.precision]conversion
最常见的应用是控制一个域的最小尺寸,这可以通过指定width来实现.
与width相对的是precision,它用来指明最大尺寸.无法应用于整数

public class Receipt {
  private double total = 0;
  private Formatter f = new Formatter(System.out);//此行很重要代表f.format()中的内容可以直接输出
  public void printTitle() {
    f.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
    f.format("%-15s %5s %10s\n", "----", "---", "-----");
  }
  public void print(String name, int qty, double price) {
    f.format("%-15.15s %5d %10.2f\n", name, qty, price);
    total += price;
  }
  public void printTotal() {
    f.format("%-15s %5s %10.2f\n", "Tax", "", total*0.06);
    f.format("%-15s %5s %10s\n", "", "", "-----");
    f.format("%-15s %5s %10.2f\n", "Total", "",
      total * 1.06);
  }
  public static void main(String[] args) {
    Receipt receipt = new Receipt();
    receipt.printTitle();
    receipt.print("Jack's Magic Beans", 4, 4.25);
    receipt.print("Princess Peas", 3, 5.1);
    receipt.print("Three Bears Porridge", 1, 14.29);
    receipt.printTotal();
  }
} /* Output:
Item              Qty      Price
----              ---      -----
Jack's Magic Be     4       4.25
Princess Peas       3       5.10
Three Bears Por     1      14.29
Tax                         1.42
                           -----
Total                      25.06
*///:~

f.format(“%-15s %5s %10.2f\n”, “Total”,”“, total * 1.06);先讲”-“标志来改变对齐方向默认时右对齐”15s”代表String占了15个字符,”5s”…..10.2f\n代表float数保留两位小数占十位

formatter

注意对于boolean基本类型或Boolean对象,其转换结果是对应的true或false,只要该参数不为null,那转换的结果就永远都是true.即使是数字0,转换结果依然为true

猜你喜欢

转载自blog.csdn.net/zhuofai_/article/details/82352682