码迷,mamicode.com
首页 > 编程语言 > 详细

Java String的点滴1

时间:2016-03-06 23:27:38      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

先来说说String特点

String是不可变的常量,每当我们创建一个字符串对象的时候,如果堆区的常量池里不存在这个字符串,就会创建一个存储在常量池里(String存的地方叫String pool),如果存在了,就直接把变量的地址指向常量池里,比如:String b = “abc” 这句话 内存表示如下。下面开始上题 

 

1.1

String s1 = new String("abc");
String s2 = new String("abc"); 
System.out.println(s1 == s2);

输出结果是什么呢? 
从上面的图也大概说了jvm里面有堆、栈区。==对于对象类型比较的是地址。所以在s1和s1是分别引用了堆里面new出来的不同对象的地址,图形理解如下

技术分享

答案很明显了,地址不同 输出false.

1.2

String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1); 
System.out.println(s1.equals(s2));

这是true 还是false呢?答案是false。

首先s1变量引用了字符串”abc”,然后StringBuffer s2 = new StringBuffer(s1),新建了一个StringBuffer对象调用append()方法返回自身。调用String的equals方法。重点就是这个equals方法里有个instance of,必需是同一类型的才进行比较否则直接返回false。 
来看一下源码:

/**
     * 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;
    }

1.3

下面的代码在内存会产生几个对象呢? 
String s1 = new String(“abc”); 
String s2 = new String(“abc”);

答案:3个 
有了上面的分析,相信大家都明白了,new了两个对象,加上string pool里的一个”abc”。




Java String的点滴1

标签:

原文地址:http://www.cnblogs.com/Kevincodebase/p/5248906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!