Java 读书笔记13 字符串

来源:互联网 发布:js base64转换二进制 编辑:程序博客网 时间:2024/05/18 00:22

字符串

不可变的字符串

早就听说java字符串是不变的;那么到底是怎么回事呢?

public class Immutable {    public  static  String upcase(String s)    {        return  s.toUpperCase();    }    public  static  void  main(String a[])    {        String q ="howdy";        System.out.println(q); //howdy        //String qq = new String("aaaa");        String qq = upcase(q);        System.out.println(qq);//HOWDY        System.out.println(q);//howdy    }}

就像上面的代码,q作为参数给了upcase 传给了qq;
我们可能回想说,你字符串q经过upcase()以后,传给qq这个新字符串,q怎么可能会不变呢?
因为,qq本来就只是一个引用,一个指向某对象的引用;

String qq = new String("aaaa");   引用指向对象的过程 

那么:

String qq = upcase(q);  //等号右面其实返回的是一个引用;一个全新对象的引用

简单来说通过q引用,找到q引用的对象,通过toUpperCase()对q对象得到一个新对象,将这个新对象的引用交给qq,万事大吉;q以及qq是引用,他们指向的对象可能会发生改变,但是实际创建的对象不会有任何改变;
如下:

String s = "abcd";      //s指向abcds = s.concat("ef");     //s指向新字符串abcdef 

String不可变

那么string为什么不可变呢?
看这里
简单而言:
1. 同一个字符串 “abc”,它在堆中就是一个对象,不管有多少所谓新的引用;
2. 其实也是java的一贯准则;更明了的让程序员知道错误;StringBuilder.append 可以改变对象值,这样会发生什么呢?当新的引用出现并改变字符串,旧的引用也就改变了;发生了连锁反应;

class  Test {    public  static  void  main(String arg[])    {        StringBuilder [] builders = new  StringBuilder[3];        StringBuilder a1=new StringBuilder("aaa");        builders[0]=a1;        StringBuilder a2 = a1;        builders[1]=a2;        StringBuilder a3 = a2;        a3.append("bbb");        builders[2]=a3;        System.out.println(Arrays.toString(builders));        //output :[aaabbb, aaabbb, aaabbb]     }}

如何实现String 不变,final+private

public final class String    implements java.io.Serializable, Comparable<String>, CharSequence {    /** The value is used for character storage. */    private final char value[];    }

所以我们知道 String不是基本类型,其实是一个类;当我们要操作字符串本身是还是使用StringBuilder 吧;另外StringBuffer为了线程同步消耗了更多的资源;所以单线程使用StringBuilder 更好;
ps:这也就是为什么Object有tostring()方法,而基本类型没有;

Formatter

public class Receipt {    Formatter formatter = new Formatter(System.out);    public  void  printtest()    {        formatter.format("%15s %5d %10.3f\n","this is a test",10,3.1564*3);        formatter.format("%15s %5d %10.2f\n","this ",10,3.1*3);    }    public  static  void  main(String a[])    {        Receipt receipt =new Receipt();        receipt.printtest();    }}

关于PrintStream

public class Turtle {    private  String name;    private Formatter formatter;    public  Turtle (String name, Formatter f)    {        this.name = name;        this.formatter = f;    }    public  void  move ( int x , int y)    {        formatter.format("%s  The Turtle is at (%d,%d)\n",name,x,y);    }    public  static void main(String a[])    {        PrintStream stream = System.err;        Turtle turtle = new Turtle("tommy",new Formatter( System.out));        Turtle turtle1 = new Turtle("tom",new Formatter(System.err));        turtle.move(5,6);        turtle.move(6,8);        turtle1.move(9,0);    }

正则表达式

这个嘛,用到再看吧

0 0
原创粉丝点击