java中的String

String 创建对象位置:
1.“ ”直接创建,返回常量池对象引用。
2.new 返回堆 中对象引用。new的String如果在常量池中存在则返回的是常量池引用
3.1: 两个常量池对象通过+,形成新的对象,返回的是常量池对象引用。如果存在则不会有新的形成,则会直接返回引用。
3.2:两个堆对象通过+形成新的对象,返回的是堆中的对象引用。
3.3.一个堆对象和常量池对象通过+形成新对象,返回的是堆中的对象引用。
intern()方法:如果常量池中不存在该字符串,则加入常量池,并返回常量池对象引用,如果存在,直接返回常量池对象引用。
只要调用intern方法就返回的是常量池的引用

String一个fina类,继承java.io.Serializable, Comparable, CharSequence

使用字符串常量池,每当我们使用字面量(String s=”1”;)创建字符串常量时,JVM会首先检查字符串常量池,如果该字符串已经存在常量池中,那么就将此字符串对象的地址赋值给引用s(引用s在Java栈中)。如果字符串不存在常量池中,就会实例化该字符串并且将其放到常量池中,并将此字符串对象的地址赋值给引用s(引用s在Java栈中)。

使用字符串常量池,每当我们使用关键字new(String s=new String(”1”);)创建字符串常量时,JVM会首先检查字符串常量池,如果该字符串已经存在常量池中,那么不再在字符串常量池创建该字符串对象,而直接堆中复制该对象的副本,然后将堆中对象的地址赋值给引用s,如果字符串不存在常量池中,就会实例化该字符串并且将其放到常量池中,然后将常量池对象的地址赋值给引用s
所以两种创建不是同一个对象,==判断时为false
new String(“a”)其实是创建了两个对象,一个为String常量池中的,类一个为堆中的实例对象。

/** The value is used for character storage. */
private final char value[];


/** Cache the hash code for the string */
private int hash; // Default to 0

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;

/**
 * Class String is special cased within the Serialization Stream Protocol.
 *
 * A String instance is written into an ObjectOutputStream according to
 * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 */
private static final ObjectStreamField[] serialPersistentFields =
    new ObjectStreamField[0];

/**
 * Initializes a newly created {@code String} object so that it represents
 * an empty character sequence.  Note that use of this constructor is
 * unnecessary since Strings are immutable.
 */
public String() {
    this.value = "".value;
}

运用char数组来存储String数据。

string的“+”
当用“+”拼接创建字符串时,会先创建一个stringbuild,把+号前的先存进去,在把+后append进去。例如string a=“1”+“3”+new string(“1”)+“4”;
会先创建一个stringbuild把1放入,然后将3append入,在创建1对象append,最后append4,然后调用stringbuild的tostring方法,返回一个string对象。

String.intern()解析
String.intern()是一个Native方法,底层调用C++的 StringTable::intern 方法,源码注释:当调用 intern 方法时,如果常量池中已经该字符串,则返回池中的字符串;否则将此字符串添加到常量池中,并返回字符串的引用。

/**
 * Returns a canonical representation for the string object.
 * <p>
 * A pool of strings, initially empty, is maintained privately by the
 * class {@code String}.
 * <p>
 * When the intern method is invoked, if the pool already contains a
 * string equal to this {@code String} object as determined by
 * the {@link #equals(Object)} method, then the string from the pool is
 * returned. Otherwise, this {@code String} object is added to the
 * pool and a reference to this {@code String} object is returned.
 * <p>
 * It follows that for any two strings {@code s} and {@code t},
 * {@code s.intern() == t.intern()} is {@code true}
 * if and only if {@code s.equals(t)} is {@code true}.
 * <p>
 * All literal strings and string-valued constant expressions are
 * interned. String literals are defined in section 3.10.5 of the
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * @return  a string that has the same contents as this string, but is
 *          guaranteed to be from a pool of unique strings.
 */
public native String intern();

在jdk1.6之前string的常量池放在永久代的,永久代和Java堆是两个完全分开的区域。而存在变量使用“+”连接而来的的对象存在Java堆中,且并未将对象存于常量池中,当调用 intern 方法时,如果常量池中已经该字符串,则返回池中的字符串;否则将此字符串添加到常量池中,并返回字符串的引用。
JDK7中,字符串常量池已经被转移至Java堆中,开发人员也对intern 方法做了一些修改。因为字符串常量池和new的对象都存于Java堆中,为了优化性能和减少内存开销,当调用 intern 方法时,如果常量池中已经存在该字符串,则返回池中字符串;否则直接存储堆中的引用,也就是字符串常量池中存储的是指向堆里的对象。

string的案例
(1)对于==,如果作用于基本数据类型的变量(byte,short,char,int,long,float,double,boolean ),则直接比较其存储的"值"是否相等;如果作用于引用类型的变量(String),则比较的是所指向的对象的地址(即是否指向同一个对象)。

(2)equals方法是基类Object中的方法,因此对于所有的继承于Object的类都会有该方法。在Object类中,equals方法是用来比较两个对象的引用是否相等。

(3)对于equals方法,注意:equals方法不能作用于基本数据类型的变量。如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;而String类对equals方法进行了重写,用来比较指向的字符串对象所存储的字符串是否相等。其他的一些类诸如Double,Date,Integer等,都对equals方法进行了重写用来比较指向的对象所存储的内容是否相等。
因为string重写的equals方法,所以他可以使用equals方法比较两个字符串的值是否相等。

/**
 * Compares this string to the specified object.  The result is {@code
 * true} if and only if the argument is not {@code null} and is a {@code
 * String} object that represents the same sequence of characters as this
 * object.
 *
 * @param  anObject
 *         The object to compare this {@code String} against
 *
 * @return  {@code true} if the given object represents a {@code String}
 *          equivalent to this string, {@code false} otherwise
 *
 * @see  #compareTo(String)
 * @see  #equalsIgnoreCase(String)
 */
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

equals方法:先判断是否为同一个对象,相同则直接返回,不相同则判断两个字符串长度是否相等,相等则循环判断存储string的char数组是否全部相等。

public static void main(String[] args) {
// write your code here
       String a="a"+"dada";
       System.out.println(a);
       /**
        * 通过“”直接创建的string和new出的不是同一个对象,一个是存在堆中,一个是存在string常量池中
        */
       String s1="a";
       String s2=new String("a");
       System.out.println(s1==s2);//false

       /**
        * 通过+拼接形成的String会通过StringBuild拼接返回一个string的实例对象,存储在堆中,跟直接“”创建不是同一个对象。
        */
       String s3="ab";
       String s4="cd";
       String s5=s3+s4;
       String s6="abcd";
       System.out.println(s5==s6);//false

       /**
        * JAVA编译器对string + 基本类型/常量 是当成常量表达式直接求值来优化的。
        * 运行期的两个string相加,会产生新的对象的,存储在堆(heap)中
        */
       String s7="ab";
       String s8=s7+"cd";
       System.out.println(s8==s6);//false

       final String s9="ab";
       String s10=s9+"cd";
       System.out.println(s10==s6);//true
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值