zoukankan      html  css  js  c++  java
  • StringBuffer类和String 类的 equals 和 ==

     注意:

     equals(Object obj)equals方法的参数是任意对象
     Object类的equals方法就是用==判断的,即判断两个对象是否为同一个对象
     StringBuffer类没有重写equals方法,String类重写了equals方法,用来判断两个字符串的内容是否相同


    以下为Object类的equals方法

    public boolean equals(Object obj) {
        return (this == obj);


    以下为String类的equals方法

     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;
        }
    public class Twine {
    
        public static void main(String[] args) {
            String s = "";
            StringBuffer sb1 = new StringBuffer("hi");
            StringBuffer sb2 = new StringBuffer("hi");
            StringBuffer sb3 = new StringBuffer(sb2);
            StringBuffer sb4 = sb3;
            if (sb1.equals(sb2)) {  //false
                s += "1";
            }
            if (sb2.equals(sb3)) {    //false
                s += "2";
            }
            if (sb3.equals(sb4)) {    //true
                s += "3";
            }
            if (sb1 == sb2) {        //false
                s += "ss";
            }
            
            //s2和s3指向同一个对象
            String s2 = "hi";
            String s3 = "hi";
            
            String s4 = s3;
            if (s2.equals(s3)) {    //true
                s += "4";
            }
            if (s3.equals(s4)) {    //true
                s += "5";
            }
            if (s2 == s3) {         //true
                s += "6";
            }            
            if (s3 == s4) {         //true
                s += "7";
            }
            if (sb1.equals(s2)) {   //false
                s += "8";
            }
            
            System.out.println(s);
            
    //        mm和nn指向的是同一个对象
    //        bb和cc指向的不是同一个对象
            
            String mm = "hi";
            String nn = "hi";
            System.out.println(mm == nn);    //true
            
            String bb = new String("hi");
            String cc = new String("hi");
            System.out.println(bb == cc);    //false
        }
    }
  • 相关阅读:
    Spring框架开发的三种模式
    IDEA 的Surround With快捷键
    Spring框架IOC和AOP的实现原理与详解
    mitmproxy 安装配置
    adb 使用
    小象代理
    requests 模块查看请求的ip地址
    smtplib 邮件模块
    淘宝直播数据爬取 + 淘宝模拟登陆
    postgresql基础操作
  • 原文地址:https://www.cnblogs.com/sodawoods-blogs/p/4396682.html
Copyright © 2011-2022 走看看