欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaSE基础知识记录(含源码分析)

程序员文章站 2024-03-23 14:46:46
...

1.==和equals有什么区别?

先看代码

  		String s1 = "hello";
        String s2 = "hello";
        System.out.println("s1:"+s1+"  s2:"+s2+"  s1==s2:"+(s1 == s2));
        System.out.println("s1:"+s1+"  s2:"+s2+"  s1.equals(s2):"+s1.equals(s2));

控制台输出

s1:hello  s2:hello  s1==s2:true
s1:hello  s2:hello  s1.equals(s2):true

我们发现好像这里的equals和==实现的功能是一样的
这时候我们再在s2下面new一个s3

		 String s3=new String("hello");
		 //再打印一次
   		System.out.println("s1:"+s1+"  s3:"+s3+"  s1==s3:"+(s1 == s3));   //false
        System.out.println("s1:"+s1+"  s3:"+s3+"  s1.equals(s3):"+s1.equals(s3)); //true

这时候我们发现为什么字符串s1和字符串s3都是hello,但是输出结果显示s1!=s2呢?

这里究其原因就是“堆”和“常量池”的不同
Sting s1="hello"是在常量池中创建一个s1字符串对象
String s3=new String(“hello”)是在堆中创建一个s3对象
如果是在常量池中使用equals函数,则调用String.equals
如果是在堆中使用equals函数,则调用Object.equals
所以在常量池中立的String.equals只比较“内容”,但是在堆中的Object.equals里面只比较“内存”。下面我们看看源码:
String.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;
    }

Object.equals

/**
     * Indicates whether some other object is "equal to" this one.
     * <p>
     * The {@code equals} method implements an equivalence relation
     * on non-null object references:
     * <ul>
     * <li>It is <i>reflexive</i>: for any non-null reference value
     *     {@code x}, {@code x.equals(x)} should return
     *     {@code true}.
     * <li>It is <i>symmetric</i>: for any non-null reference values
     *     {@code x} and {@code y}, {@code x.equals(y)}
     *     should return {@code true} if and only if
     *     {@code y.equals(x)} returns {@code true}.
     * <li>It is <i>transitive</i>: for any non-null reference values
     *     {@code x}, {@code y}, and {@code z}, if
     *     {@code x.equals(y)} returns {@code true} and
     *     {@code y.equals(z)} returns {@code true}, then
     *     {@code x.equals(z)} should return {@code true}.
     * <li>It is <i>consistent</i>: for any non-null reference values
     *     {@code x} and {@code y}, multiple invocations of
     *     {@code x.equals(y)} consistently return {@code true}
     *     or consistently return {@code false}, provided no
     *     information used in {@code equals} comparisons on the
     *     objects is modified.
     * <li>For any non-null reference value {@code x},
     *     {@code x.equals(null)} should return {@code false}.
     * </ul>
     * <p>
     * The {@code equals} method for class {@code Object} implements
     * the most discriminating possible equivalence relation on objects;
     * that is, for any non-null reference values {@code x} and
     * {@code y}, this method returns {@code true} if and only
     * if {@code x} and {@code y} refer to the same object
     * ({@code x == y} has the value {@code true}).
     * <p>
     * Note that it is generally necessary to override the {@code hashCode}
     * method whenever this method is overridden, so as to maintain the
     * general contract for the {@code hashCode} method, which states
     * that equal objects must have equal hash codes.
     *
     * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

根据上面源码我们很容易得出来Object.equals里面的equals其实就是==写出来的。但是String.equals首先判断内存相不相等,如果相等,直接返回true,如果内存不等,就开始判断两个字符串是否内容相等。

2.最近新学Python和重学javaSE,发现其实很多Python里面好用的函数,JavaSE里面也有

比如一些切割函数:
JavaSE基础知识记录(含源码分析)
我就直接放作者链接了:
https://www.liaoxuefeng.com/wiki/1252599548343744/1260469698963456

相关标签: javase java