java源码之String

 

1.String源码中队String类的描述:

   The String class represents character strings. All string literals in Java programs, such as "abc", are

implemented as instances of this class.

   Strings are constant; their values cannot be changed after they are created.String buffers support mutable strings.Because String objects are immutable they can be shared.

 (注意这一句:因为String是不可变的,所以String字符串能够共享)

 

2. String类的定义:

    public final class String...,因为是final的所以不能被其它类继承。

    主要有四个变量:

       value[] 字符数组 主要存放字符串的值;String和StringBuffer的可变和不可变本质上是体现在这个字段上的。

       offset  字符串的当前索引;

       count   字符串的长度;

       hash   字符串的hash码;

 

       当new一个空对象的时候 ,因为String不可变,所以new 一个String是没有多大意思的,如:

     public String() {

	this.offset = 0; // 字符串第一位索引
	this.count = 0;  // 字符串总数
	this.value = new char[0];  // 字符数组
      }

 


      // 带参数的构造方法

      public String(String original) {

		int size = original.count;
		char[] originalValue = original.value;
		char[] v;
		if (originalValue.length > size) {
		    // The array representing the String is bigger than the new
		    // String itself.  Perhaps this constructor is being called
		    // in order to trim the baggage, so make a copy of the array.
		    int off = original.offset;
		    v = Arrays.copyOfRange(originalValue, off, off+size);
		} else {
		    // The array representing the String is the same
		    // size as the String, so no point in making a copy.
		    v = originalValue;
		}
		this.offset = 0;
		this.count = size;
		this.value = v;
	 }

 

3. String不可变和StringBuffer可变

                String s1= new String("abc");

		String s2 = s1;
		System.out.println(s2 == s1); // 返回true
		s2 = s2+"d";
		System.out.println(s2 == s1);// 返回false,问什么会这样那,下面会有解释
		
		StringBuffer sb1 = new StringBuffer("abc");
		StringBuffer sb2 = sb1;
		System.out.println(sb1 == sb2);// 返回true
		sb2.append("d");
		System.out.println(sb1 == sb2);// 返回true

 


   

   对于s2 = s2+"d";的处理步骤实际上是通过建立一个StringBuffer(有的地方说是创建一个StringBuilder),然后调用append(),

     最后再将StringBuffer toSting();所以str = str + "d";可以等同于:

    StringBuffer sb = new StringBuffer(s2);  

    s2.append("d");  
    s2 = s2.toString();

 

     关键是最后一步,StringBuffer的toString方法是重新new 一个String对象。源代码:

     public synchronized String toString() {

	return new String(value, 0, count);
     }

 

     String 有一个常量池在方法区中(存放的是一些classes文件),不是在堆中(存放new出来的对象),每当new一个String对象的时候,都会先到字符串常量池中去找,如果没找到,则在常量池中 创建一个String的对象,然后再在堆中创建一个String对象。如果在常量池中找到了,则直接在堆中创建一个对象。

     String声明式创建对象,就是用new创建。如String s = new String("123");

     String隐含式创建对象,如:String s = "123"; StringBuffer不能这样赋值,只能用new。

     上面的两种方式是不同的。

     隐含式创建对象不会再堆中创建对象。而是引用常量池中的对象(如果常量池没有,则在常量池中创建一个对象,不会再堆中创建);

     String s4 = "hello";

     String s5 = "a" + "b" + s4; 
     String s6 = s4 + "b" + "a";

 

      上面s4和s5的创建过程是不同的。

      s5在编译的时候会优化成String s5 = "ab" + s4; 

      然后

          StringBuffer sb = new StringBuffer("ab");

           sb.append(s4);
	   return sb.toString();

 

      而s6不会优化,因为字符编译的顺序是从左到右的。

          StringBuffer sb = new StringBuffer(s4);

           sb.append("a");
	   return sb.toString();
	   
	   StringBuffer sb = new StringBuffer("hellob"); 
	   sb.append("a");
           return sb.toString();
      

 

 

4. StringBuffer:

    文档说明:

    A thread-safe, mutable sequence of characters. 

    A string buffer is like a  String}, but can be modified. At any 

    point in time it contains some particular sequence of characters, but 

    the length and content of the sequence can be changed through certain 

    method calls.

    StringBuf 默认是一个16位的字符数组:

           public StringBuffer() {

	         super(16); // 调用父类的AbstractStringBuilder方法,下面的这个方法
            }

 

     AbstractStringBuilder(int capacity) {

		value = new char[capacity];
	     }

 


      StringBuffer 的appen的方法

      public AbstractStringBuilder append(String str) {

		if (str == null) str = "null";
		int len = str.length();
		if (len == 0) return this;
		int newCount = count + len;
		if (newCount > value.length)
		    expandCapacity(newCount); // 增加数组的长度。实际上是新建一个数组,把原来数组的值拷贝到新数组中
		str.getChars(0, len, value, count); // 把str的值拷贝到value中
		count = newCount;
		return this;
         }

 














 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值